CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Convert float number to PIC 4 byte hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Dave Mac
Guest







Convert float number to PIC 4 byte hex
PostPosted: Mon Jan 20, 2003 4:18 pm     Reply with quote

I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
example
input 3.14159
output 80490FD0

just need the conversion routine (in C). thanks
___________________________
This message was ported from CCS's old forum
Original Post ID: 10837
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: Convert float number to PIC 4 byte hex
PostPosted: Mon Jan 20, 2003 4:39 pm     Reply with quote

:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=example
:=input 3.14159
:=output 80490FD0
:=
:=just need the conversion routine (in C). thanks
-------------------------------------------------------
I found this by doing a search for "float" and "convert".
<a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10838
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: Convert float number to PIC 4 byte hex
PostPosted: Mon Jan 20, 2003 5:06 pm     Reply with quote

:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=example
:=:=input 3.14159
:=:=output 80490FD0
:=:=
:=:=just need the conversion routine (in C). thanks
:=-------------------------------------------------------
:=I found this by doing a search for "float" and "convert".
:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a>

This one helps as well
<a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10841
Dave Mac
Guest







Re: Convert float number to PIC 4 byte hex
PostPosted: Mon Jan 20, 2003 5:40 pm     Reply with quote

<font face="Courier New" size=-1>:=:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=:=example
:=:=:=input 3.14159
:=:=:=output 80490FD0
:=:=:=
:=:=:=just need the conversion routine (in C). thanks
:=:=-------------------------------------------------------
:=:=I found this by doing a search for "float" and "convert".
:=:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a></a></a></a></a>
:=
:=This one helps as well
:= <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a></a></a></a></a>

thanks guys, I will try it out. (I needed the second example from Neutone). I'll let you know.


-dave</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 10842
Dave Mac
Guest







Re: Convert float number to PIC 4 byte hex
PostPosted: Wed Jan 22, 2003 3:28 pm     Reply with quote

<font face="Courier New" size=-1>:=<font face="Courier New" size=-1>:=:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=:=:=example
:=:=:=:=input 3.14159
:=:=:=:=output 80490FD0
:=:=:=:=
:=:=:=:=just need the conversion routine (in C). thanks
:=:=:=-------------------------------------------------------
:=:=:=I found this by doing a search for "float" and "convert".
:=:=:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a></a></a></a></a></a></a>
:=:=
:=:=This one helps as well
:=:= <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a></a></a></a></a></a></a>
:=
:=thanks guys, I will try it out. (I needed the second example from Neutone). I'll let you know.
:=
:=
:=-dave</font>

I tried the c example in apnote 575 from microchip it worked on some numbers but not others, any coments?

#include stdio.h
#include math.h
main(){

double m,e,s,x,f0,f1,f2;
unsigned long a,b,c,d ;

printf("Enter x: ");
while(scanf("\%lf",&x) == 1) {

if (x < 0){
x = -x;
s = 128; //negitive flag
}

e = (int)Log(x) / Log(2) ;
m = x / pow(2.,e) ;
m = m - 1 ;

m = m * pow(2.,7) + s ;
f0 = (int)m ;
m = (m - f0) * pow(2.,8) ;
f1 = (int)m ;
m = (m - f1) * pow(2.,8) ;
f2 = (int)m ;
e = e + 127 ;

a = e;
b = f0;
c = f1;
d = f2;

printf("\%X,\%X,\%X,\%X\n",a,b,c,d);

printf("\n");
printf("Enter A: ");
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 10899
TSchultz



Joined: 08 Sep 2003
Posts: 66
Location: Toronto, Canada

View user's profile Send private message

RE: Convert float number to PIC 4 byte hex
PostPosted: Wed Jan 22, 2003 4:27 pm     Reply with quote

:=<font face="Courier New" size=-1>:=<font face="Courier New" size=-1>:=:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=:=:=:=example
:=:=:=:=:=input 3.14159
:=:=:=:=:=output 80490FD0
:=:=:=:=:=
:=:=:=:=:=just need the conversion routine (in C). thanks
:=:=:=:=-------------------------------------------------------
:=:=:=:=I found this by doing a search for "float" and "convert".
:=:=:=:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a></a></a></a></a></a></a></a>
:=:=:=
:=:=:=This one helps as well
:=:=:= <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a></a></a></a></a></a></a></a>
:=:=
:=:=thanks guys, I will try it out. (I needed the second example from Neutone). I'll let you know.
:=:=
:=:=
:=:=-dave</font>
:=
:=I tried the c example in apnote 575 from microchip it worked on some numbers but not others, any coments?
:=
:=#include stdio.h
:=#include math.h
:=main(){
:=
:=double m,e,s,x,f0,f1,f2;
:=unsigned long a,b,c,d ;
:=
:=printf("Enter x: ");
:= while(scanf("\%lf",&x) == 1) {
:=
:= if (x < 0){
:= x = -x;
:= s = 128; //negitive flag
:= }
:=
:= e = (int)Log(x) / Log(2) ;
:= m = x / pow(2.,e) ;
:= m = m - 1 ;
:=
:= m = m * pow(2.,7) + s ;
:= f0 = (int)m ;
:= m = (m - f0) * pow(2.,8) ;
:= f1 = (int)m ;
:= m = (m - f1) * pow(2.,8) ;
:= f2 = (int)m ;
:= e = e + 127 ;
:=
:= a = e;
:= b = f0;
:= c = f1;
:= d = f2;
:=
:= printf("\%X,\%X,\%X,\%X\n",a,b,c,d);
:=
:= printf("\n");
:= printf("Enter A: ");
:= }
:=}
:=

This is not exactly what you are looking for but in some of my old archives I have these notes. I am not sure if I tested these but here is how you can very easily convert from the IEEE to PIC floating point format. This is PIC assembly;

IEEE to PIC floating point conversion
RLF MSB ;move most significant bit to carry
RLF EXP ;put bit at top of EXP and get sign bit in carry
RRF MSB ;move sign bit into MSB


IEEE to PIC floating point conversion
RLF MSB ;move sign bit to carry
RRF EXP ;put sign at top of EXP and get most significant bit in carry
RRF MSB ;move most significant bit into MSB

If you change your concept and convert in the PIC after receive from the PC this would work.

If you want I can also provide tested and working functions done in Visual Basic.

Also keep in mind there are some differences in the byte ordering, little endian vs big endian, depending on how you transfer the bytes between the PC and the PIC.

-Troy
___________________________
This message was ported from CCS's old forum
Original Post ID: 10908
R.J.Hamlett
Guest







Re: Convert float number to PIC 4 byte hex
PostPosted: Thu Jan 23, 2003 11:19 am     Reply with quote

:=<font face="Courier New" size=-1>:=<font face="Courier New" size=-1>:=:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=:=:=:=example
:=:=:=:=:=input 3.14159
:=:=:=:=:=output 80490FD0
:=:=:=:=:=
:=:=:=:=:=just need the conversion routine (in C). thanks
:=:=:=:=-------------------------------------------------------
:=:=:=:=I found this by doing a search for "float" and "convert".
:=:=:=:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a></a></a></a></a></a></a></a>
:=:=:=
:=:=:=This one helps as well
:=:=:= <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a></a></a></a></a></a></a></a>
:=:=
:=:=thanks guys, I will try it out. (I needed the second example from Neutone). I'll let you know.
:=:=
:=:=
:=:=-dave</font>
:=
:=I tried the c example in apnote 575 from microchip it worked on some numbers but not others, any coments?
:=
:=#include stdio.h
:=#include math.h
:=main(){
:=
:=double m,e,s,x,f0,f1,f2;
:=unsigned long a,b,c,d ;
:=
:=printf("Enter x: ");
:= while(scanf("\%lf",&x) == 1) {
:=
:= if (x < 0){
:= x = -x;
:= s = 128; //negitive flag
:= }
:=
:= e = (int)Log(x) / Log(2) ;
:= m = x / pow(2.,e) ;
:= m = m - 1 ;
:=
:= m = m * pow(2.,7) + s ;
:= f0 = (int)m ;
:= m = (m - f0) * pow(2.,8) ;
:= f1 = (int)m ;
:= m = (m - f1) * pow(2.,8) ;
:= f2 = (int)m ;
:= e = e + 127 ;
:=
:= a = e;
:= b = f0;
:= c = f1;
:= d = f2;
:=
:= printf("\%X,\%X,\%X,\%X\n",a,b,c,d);
:=
:= printf("\n");
:= printf("Enter A: ");
:= }
:=}
:=
Not 'C', but the following code does the conversion for me in Visual Basic, and has worked for all values that I have tried...

Function tovb(inval As Single) As Single
'Here I have a 'single' comprising four bytes, that are from the remote system
'This needs to be reorganised to convert to the VB format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) / 2&
'Now retrieve the sign bit
sign = bytes(2) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127)
If (val And 128) Then
bytes(2) = bytes(2) Or 128
End If
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) Or sign
CopyMemory rval, bytes(0), 4
tovb = rval
End Function

Function toccs(inval As Single) As Single
'This is the reverse conversion to CCS format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) * 2&
'Now retrieve the sign bit
sign = bytes(3) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127) Or sign
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) And 255
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
CopyMemory rval, bytes(0), 4
toccs = rval
End Function
___________________________
This message was ported from CCS's old forum
Original Post ID: 10940
R.J.Hamlett
Guest







Re: Convert float number to PIC 4 byte hex
PostPosted: Thu Jan 23, 2003 11:36 am     Reply with quote

:=<font face="Courier New" size=-1>:=<font face="Courier New" size=-1>:=:=:=I need some c source code to convert a real number(float) to CCS 4 byte hex. This will be done on a pc and then download 4 bytes in ascii hex (8 ascii characters) to a non pic microcontroller that will burn into serail eeprom just as if CCS had put a flaot variable there (4bytes). Then a PIC microcontroller will read those 4 bytes into a float variable location and use it as normal.
:=:=:=:=:=example
:=:=:=:=:=input 3.14159
:=:=:=:=:=output 80490FD0
:=:=:=:=:=
:=:=:=:=:=just need the conversion routine (in C). thanks
:=:=:=:=-------------------------------------------------------
:=:=:=:=I found this by doing a search for "float" and "convert".
:=:=:=:= <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/132.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/132.html</a></a></a></a></a></a></a></a></a>
:=:=:=
:=:=:=This one helps as well
:=:=:= <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank"> <a href="http://www.pic-c.com/forum/general/posts/1088.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/1088.html</a></a></a></a></a></a></a></a>
:=:=
:=:=thanks guys, I will try it out. (I needed the second example from Neutone). I'll let you know.
:=:=
:=:=
:=:=-dave</font>
:=
:=I tried the c example in apnote 575 from microchip it worked on some numbers but not others, any coments?
:=
:=#include stdio.h
:=#include math.h
:=main(){
:=
:=double m,e,s,x,f0,f1,f2;
:=unsigned long a,b,c,d ;
:=
:=printf("Enter x: ");
:= while(scanf("\%lf",&x) == 1) {
:=
:= if (x < 0){
:= x = -x;
:= s = 128; //negitive flag
:= }
:=
:= e = (int)Log(x) / Log(2) ;
:= m = x / pow(2.,e) ;
:= m = m - 1 ;
:=
:= m = m * pow(2.,7) + s ;
:= f0 = (int)m ;
:= m = (m - f0) * pow(2.,8) ;
:= f1 = (int)m ;
:= m = (m - f1) * pow(2.,8) ;
:= f2 = (int)m ;
:= e = e + 127 ;
:=
:= a = e;
:= b = f0;
:= c = f1;
:= d = f2;
:=
:= printf("\%X,\%X,\%X,\%X\n",a,b,c,d);
:=
:= printf("\n");
:= printf("Enter A: ");
:= }
:=}
:=
Not 'C', but the following code does the conversion for me in Visual Basic, and has worked for all values that I have tried...
Visual basic doesn't have a union, or similar method of converting between bytes and the actual single, so most of the code is the use of the 'CopyMemory' function to transfer the value to an array of bytes, and then manipulate these, and convert back...

Function tovb(inval As Single) As Single
'Here I have a 'single' comprising four bytes, that are from the remote system
'This needs to be reorganised to convert to the VB format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) / 2&
'Now retrieve the sign bit
sign = bytes(2) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127)
If (val And 128) Then
bytes(2) = bytes(2) Or 128
End If
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) Or sign
CopyMemory rval, bytes(0), 4
tovb = rval
End Function

Function toccs(inval As Single) As Single
'This is the reverse conversion to CCS format.
Dim bytes(0 To 3) As Byte
Dim val As Long
Dim sign As Integer
Dim rval As Single
CopyMemory bytes(0), inval, 4
'First generate the 16bit value corresponding to the two high bytes, and move the exponent
'to it's final position.
val = (CLng(bytes(2)) + (CLng(bytes(3)) * 256&)) * 2&
'Now retrieve the sign bit
sign = bytes(3) And 128
'Mask off, and put in the bottom bit of the exponent
bytes(2) = (bytes(2) And 127) Or sign
'Now extract the top seven bits of the exponent
bytes(3) = (val \ 256) And 255
swap bytes(0), bytes(3)
swap bytes(1), bytes(2)
CopyMemory rval, bytes(0), 4
toccs = rval
End Function
___________________________
This message was ported from CCS's old forum
Original Post ID: 10941
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group