|
|
View previous topic :: View next topic |
Author |
Message |
Geert Boone Guest
|
the format of floats |
Posted: Tue Nov 13, 2001 5:55 am |
|
|
hello
in the book i' ve seen that
the float 123.45 has the following code : 85 48 E6 66
I don't understand this Hex codes .
can someone explain how you can see the 123.45 in the hex codes?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1081 |
|
|
Tomi Guest
|
Re: the format of floats |
Posted: Tue Nov 13, 2001 7:03 am |
|
|
According to the Convert Utility, the number "123.45" has the following hex code:
85 76 E6 66
See the Application Note AN575 for the Microchip modified IEEE754 32-bit floating point format.
:=hello
:=
:=in the book i' ve seen that
:=
:=the float 123.45 has the following code : 85 48 E6 66
:=I don't understand this Hex codes .
:=can someone explain how you can see the 123.45 in the hex codes?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1082 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: the format of floats |
Posted: Tue Nov 13, 2001 12:55 pm |
|
|
The following (a snip from a VB application) assumes a byte array called EEPROM with 5 floating point numbers and converts the bytes to floats. The floats are then loaded into a text array.
For y = 0 To 4
If EEPROM(1 + (y * 4)) > 127 Then
x = -1 * (2 ^ (EEPROM(0 + (y * 4)) - 127)) * (((EEPROM(1 + (y * 4))) * (2 ^ 16)) + (EEPROM(2 + (y * 4)) * (2 ^ 8)) + EEPROM(3 + (y * 4))) / (2 ^ 23)
Else
x = (2 ^ (EEPROM(0 + (y * 4)) - 127)) * (((EEPROM(1 + (y * 4)) + 128) * (2 ^ 16)) + (EEPROM(2 + (y * 4)) * (2 ^ 8)) + EEPROM(3 + (y * 4))) / (2 ^ 23)
End If
TxtNumber(y).Text = Format(x, "#0.0###############")
Next
The following (a snip from a VB application) assumes 5 floating point numbers and converts to a byte array called EEPROM.
x = Val(TxtNumber(n).Text)
If x < 0 Then
x = -x
s = 128 'negitive flag
End If
e = Int(Log(x) / Log(2))
m = x / (2 ^ e)
m = m - 1
m = m * (2 ^ 7) + s
f0 = Int(m)
m = (m - f0) * (2 ^ 8)
f1 = Int(m)
m = (m - f1) * (2 ^ 8)
f2 = Int(m)
e = e + 127
EEPROM(0 + (n * 4)) = e
EEPROM(1 + (n * 4)) = f0
EEPROM(2 + (n * 4)) = f1
EEPROM(3 + (n * 4)) = f2
Next
:=hello
:=
:=in the book i' ve seen that
:=
:=the float 123.45 has the following code : 85 48 E6 66
:=I don't understand this Hex codes .
:=can someone explain how you can see the 123.45 in the hex codes?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1088 |
|
|
Karel Hladky Guest
|
Re: the format of floats |
Posted: Tue Nov 13, 2001 9:48 pm |
|
|
:=According to the Convert Utility, the number "123.45" has the following hex code:
:=85 76 E6 66
:=See the Application Note AN575 for the Microchip modified IEEE754 32-bit floating point format.
:=
:=:=hello
:=:=
:=:=in the book i' ve seen that
:=:=
:=:=the float 123.45 has the following code : 85 48 E6 66
:=:=I don't understand this Hex codes .
:=:=can someone explain how you can see the 123.45 in the hex codes?
You must be looking at the CCS manual. It's had this misprint in there as long as I can remember. The value for 100 is also wrong - should be 85 48 00 00. I'd tried to get them to correct it, but CCs just couldn't care less. Wouldn't be surprised if in their complier it actually evaluated to this :(
Have a look at fconvert - <a href="http://www.al-williams.com/awce.html" TARGET="_blank">http://www.al-williams.com/awce.html</a>
Karel
___________________________
This message was ported from CCS's old forum
Original Post ID: 1092 |
|
|
Karel Hladky Guest
|
Re: the format of floats |
Posted: Tue Nov 13, 2001 9:58 pm |
|
|
:=Have a look at fconvert - sorry, try this link instead :
<a href="http://www.al-williams.com/awce/" TARGET="_blank">http://www.al-williams.com/awce/</a>
or directly :
<a href="http://www.al-williams.com/awce/fconvert.zip" TARGET="_blank">http://www.al-williams.com/awce/fconvert.zip</a>
Karel
___________________________
This message was ported from CCS's old forum
Original Post ID: 1093 |
|
|
TSchultz
Joined: 08 Sep 2003 Posts: 66 Location: Toronto, Canada
|
Re: the format of floats |
Posted: Wed Nov 14, 2001 8:23 am |
|
|
:=hello
:=
:=in the book i' ve seen that
:=
:=the float 123.45 has the following code : 85 48 E6 66
:=I don't understand this Hex codes .
:=can someone explain how you can see the 123.45 in the hex codes?
Here are a couple of VB routines if you are handling binary packed data. At the top of both functions, are the PIC instructions necessary to handle the format conversion. Please note that these VB routines also handle re-ordering of the bytes.
The routines are not as elegant as you can do in C, since VB has no bit manipulation functions, but they get the job done.
The PIC uses the same byte ordering that Motorolla uses, while of course VB uses Intels byte ordering (they are different) "Little Endian vs Big Endian"
There is also the definition for the CopyMemory API call used in the routines, it comes in very handly, but you must be carefull when using it to make sure you know how many bytes you are copying and that the destination has room for those bytes. It provides almost the same functionality in VB as casting does in C when used properly.
****Start of VB code
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal BytesToCopy As Long)
Public Function FloatIntelIEEEtoPIC(num As Single) As Single
'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
'
'Note: Intel has bytes in reverse order compared to PIC, bits in each byte are
' correctly ordered.
Dim b(4) As Byte
Dim sign As Byte
Dim carry As Byte
Dim i As Byte
Dim n As Single
'Get each byte of the floating point number
CopyMemory b(0), num, LenB(num)
'Reverse byte order for EXP, MSB, ..., LSB (Intel is oposite PIC)
i = b(0): b(0) = b(3): b(3) = i
i = b(1): b(1) = b(2): b(2) = i
sign = Abs((b(0) And 128) >= 128) 'get sign bit
carry = Abs((b(1) And 128) >= 128) 'get highest bit from MSB
b(0) = ((b(0) And 127) * 2) + carry 'rotate EXP left and add carry bit
b(1) = (b(1) And 127) + (128 * sign) 'sign bit if highest bit of MSB
'Put each byte in a floating point variable
CopyMemory n, b(0), LenB(num)
'Return the variable
FloatIntelIEEEtoPIC = n
End Function
Public Function FloatPICtoIntelIEEE(num As Single) As Single
'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
'
'Note: Intel has bytes in reverse order compared to PIC, bits in each byte are
' correctly ordered.
Dim b(4) As Byte
Dim sign As Byte
Dim carry As Byte
Dim i As Byte
Dim n As Single
'Get each byte of the floating point number
CopyMemory b(0), num, LenB(num)
sign = Abs((b(1) And 128) >= 128) 'get sign bit
carry = b(0) And 1 'get lowest bit from EXP
b(0) = ((b(0) And 254) \ 2) + (sign * 128) 'rotate EXP right and add sign bit
b(1) = ((b(1) And 127)) + (carry * 128) 'put highest bit of MSB
'Reverse byte order for LSB, ..., MSB, EXP (Intel is oposite PIC)
i = b(0): b(0) = b(3): b(3) = i
i = b(1): b(1) = b(2): b(2) = i
'Put each byte in a floating point variable
CopyMemory n, b(0), LenB(num)
'Return the variable
FloatPICtoIntelIEEE = n
End Function
***End of VB code
___________________________
This message was ported from CCS's old forum
Original Post ID: 1095 |
|
|
H0E Guest
|
Re: the format of floats |
Posted: Mon Nov 19, 2001 12:49 pm |
|
|
Sorry to answer a quaetion with a problem, but I too am stuck on this. Serial device spews out IEEE floats in ascii format. Damn cannot figure out how to do it?
Dave
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(){
// ****************************************************
// Purpose: Convert 4 bytes to an IEEE 4byte Float
// ****************************************************
// Accepts 4 bytes in keyboard and convets them to show
// as a IEEE FLOAT FORMAT (4 BYTE) value.
//
// by David, immtech@yahoo.com
// ****************************************************
int bytes[4] ; // ="AF" etc
int i;
unsigned char *fch;
float floatnum;
int retval;
fch = (unsigned char*)&floatnum; // fch equals Address of floatnum, increment thru 4 bytes
for(;;){
for (i=0;i <=3;i++){
printf("Enter Hex Number to convert to float.\n");
printf("Enter byte (Using '0X' Hex prefix) #\%x = ", i);
retval = scanf( "\%x", &bytes[i] );
printf("Received \%d byte which of value 0X\%x \n\n", retval, bytes[i]);
*fch = (unsigned char)bytes[i]; // Put bytes[i] into memory address point to by fch
fch++;
}
printf("\n IEEE Format (Hex) = 0x\%2.2X\%2.2X\%2.2X\%2.2X \n", bytes[0], bytes[1], bytes[2], bytes[3]);
printf(" Float = \%f\n\n", floatnum);
}
}
:=hello
:=
:=in the book i' ve seen that
:=
:=the float 123.45 has the following code : 85 48 E6 66
:=I don't understand this Hex codes .
:=can someone explain how you can see the 123.45 in the hex codes?
___________________________
This message was ported from CCS's old forum
Original Post ID: 1234 |
|
|
|
|
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
|