473,651 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting 64-bit fixed-point to float

Hi Group,

troubles with converting signed 32.32, little-endian, 2's complement
back to floating point. I have been trying to brew it myself. I am
running Python 2.5 on a Mac. Here is the C-code I have been trying to
leverage:

double FPuint8ArrayToF PDouble(uint8 *buffer, int startIndex)
{
uint32 resultDec = 0;
uint32 resultWh = 0;
int i;

for(i = 0; i < 4; i++)
{
resultDec += (uint32)buffer[startIndex + i] * pow(2, (i*8));
resultWh += (uint32)buffer[startIndex + i + 4] * pow(2, (i*8));
}

return ( (double)((int)r esultWh) + (double)(result Dec)/4294967296.0 );
}
Here is my version in Python, with some test code built in:

from ctypes import *

def conv64(input):
input1=[0]*8
input1[0]=c_ushort(input[0])
input1[1]=c_ushort(input[1])
input1[2]=c_ushort(input[2])
input1[3]=c_ushort(input[3])
input1[4]=c_ushort(input[4])
input1[5]=c_ushort(input[5])
input1[6]=c_ushort(input[6])
input1[7]=c_ushort(input[7])
#print input1[0].value,
input1[1].value,input1[2].value,input1[3].value
#print
input1[4].value,input1[5].value,input1[6].value,input1[7].value
#print
resultDec=c_ulo ng(0)
resultWh=c_ulon g(0)
for i in range(4):
dec_c=c_ulong(i nput1[i].value)
Wh_c=c_ulong(in put1[i+4].value)
resultDec.value =resultDec.valu e+dec_c.value*2 **(i*8)
resultWh.value= resultWh.value+ Wh_c.value*2**( i*8)
conval=float(in t(resultWh.valu e))+float(resul tDec.value)/4294967296.0
#print conval
return conval
#tabs got messed up bringing this into MacSoup

#these are 64-bit fixed point format (signed 32.32, little-endian, 2's
complement)
#should be -1
conv64_0=[0, 0, 0, 255, 255, 255, 255, 255]
#should be 0
conv64_1=[0, 0, 0, 0, 0, 0, 0, 0]
#should be 0.20000
conv64_1_2=[51, 51, 51, 51, 0, 0, 0, 0]
#should be 1
conv64_2=[0, 0, 0, 0, 1, 0, 0, 0]
#should be 2
conv64_3=[0, 0, 0, 0, 2, 0, 0, 0]
#should be 298.15
conv64_4=[102, 102, 102, 38, 42, 1, 0, 0]
#should be -0.2
conv64_5=[205,204,204,204 ,255,255,255,25 5]
output0=conv64( conv64_0)
print "output should be -1 is "+str(outpu t0)
output1=conv64( conv64_1)
print "output should be 0 is "+str(outpu t1)
output1_2=conv6 4(conv64_1_2)
print "output should be 0.2 is "+str(output1_2 )
output2=conv64( conv64_2)
print "output should be 1 is "+str(outpu t2)
output3=conv64( conv64_3)
print "output should be 2 is "+str(outpu t3)
output4=conv64( conv64_4)
print "output should be 298.15 is "+str(outpu t4)
output5=conv64( conv64_5)
print "output should be -0.2 is "+str(outpu t5)

Finally, here is the output I get from my code:
>>>
output should be -1 is 4294967296.0
output should be 0 is 0.0
output should be 0.2 is 0.199999999953
output should be 1 is 1.0
output should be 2 is 2.0
output should be 298.15 is 298.15
output should be -0.2 is 4294967295.8

Thanks for any light you can shed on my ignorance.

wave_man
Jul 21 '07 #1
2 2879
It appears to be correct for positive numbers.

if conval >= 2**16:
conval -= 2**32

would appear to patch things up.

It's not very pretty, though. You could at least start with

input1 = [c_ushort(item) for item in input]

instead of your first 9 lines.

mt

Jul 21 '07 #2
On Jul 20, 5:59 pm, johnmfis...@com cast.net (John Fisher) wrote:
Hi Group,

troubles with converting signed 32.32, little-endian, 2's complement
back to floating point. I have been trying to brew it myself. I am
running Python 2.5 on a Mac. Here is the C-code I have been trying to
leverage:

double FPuint8ArrayToF PDouble(uint8 *buffer, int startIndex)
{
uint32 resultDec = 0;
uint32 resultWh = 0;
int i;

for(i = 0; i < 4; i++)
{
resultDec += (uint32)buffer[startIndex + i] * pow(2, (i*8));
resultWh += (uint32)buffer[startIndex + i + 4] * pow(2, (i*8));
}

return ( (double)((int)r esultWh) + (double)(result Dec)/4294967296.0 );

}

There are a few problem spots in that C code. I tend to
think that it "works" because you're on a system that has
4-byte int and CHAR_BIT == 8. When the most-significant-bit (MSB)
of resultWh is 1, then casting to int makes that a negative
value (i.e., MSB == the sign bit).

I presume that somewhere you include <stdint.h(fro m C99)
and that uint32 is really uint32_t, etc. For that to be
portable, you should probably cast to int32_t?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

double arr2dbl (uint8_t *buffer, int startIndex)
{
uint32_t decimal = 0;
uint32_t whole = 0;
size_t i;
for (i = 0; i < 4; ++i)
{
decimal += (buffer[startIndex + i] << (i*8));
whole += (buffer[startIndex + i + 4] << (i*8));
}
return (int32_t)whole + (decimal/(UINT32_MAX+1.0 ));
}

int main (void)
{
uint8_t arr[7][8] = {
{0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff},
{0, 0, 0, 0, 0, 0, 0, 0},
{51, 51, 51, 51, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 2, 0, 0, 0},
{102, 102, 102, 38, 42, 1, 0, 0 },
{205, 204, 204, 204, 0xff, 0xff, 0xff, 0xff}};
size_t i;
double result;
for (i = 0; i < sizeof arr/sizeof arr[0]; ++i)
{
result = arr2dbl(arr[i], 0);
printf("%f\n", result);
}
return 0;
}


Here is my version in Python, with some test code built in:

from ctypes import *

def conv64(input):
input1=[0]*8
input1[0]=c_ushort(input[0])
input1[1]=c_ushort(input[1])
input1[2]=c_ushort(input[2])
input1[3]=c_ushort(input[3])
input1[4]=c_ushort(input[4])
input1[5]=c_ushort(input[5])
input1[6]=c_ushort(input[6])
input1[7]=c_ushort(input[7])
#print input1[0].value,
input1[1].value,input1[2].value,input1[3].value
#print
input1[4].value,input1[5].value,input1[6].value,input1[7].value
#print
resultDec=c_ulo ng(0)
resultWh=c_ulon g(0)
for i in range(4):
dec_c=c_ulong(i nput1[i].value)
Wh_c=c_ulong(in put1[i+4].value)
resultDec.value =resultDec.valu e+dec_c.value*2 **(i*8)
resultWh.value= resultWh.value+ Wh_c.value*2**( i*8)
conval=float(in t(resultWh.valu e))+float(resul tDec.value)/4294967296.0
#print conval
return conval
#tabs got messed up bringing this into MacSoup

(snipped)

>
Finally, here is the output I get from my code:

output should be -1 is 4294967296.0
output should be 0 is 0.0
output should be 0.2 is 0.199999999953
output should be 1 is 1.0
output should be 2 is 2.0
output should be 298.15 is 298.15
output should be -0.2 is 4294967295.8

Thanks for any light you can shed on my ignorance.

wave_man

This is my translation:

from ctypes import *

def conv64(input):
input1 = [c_uint8(item) for item in input]
dec = c_uint32(0)
whl = c_uint32(0)
for i in xrange(4):
dec.value += (input1[i].value << (i*8))
whl.value += (input1[i+4].value << (i*8))
cast_whl_to_int = c_int32(whl.val ue)
return float(cast_whl_ to_int.value + dec.value/4294967296.0)
for arr in [[0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff],
[0, 0, 0, 0, 0, 0, 0, 0],
[51, 51, 51, 51, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 2, 0, 0, 0],
[102, 102, 102, 38, 42, 1, 0, 0],
[205,204,204,204 ,255,255,255,25 5]]:
print "%f" % conv64(arr)

However, I've not looked deeply into ctypes so I
don't know if c_int32 is really C's int, or int32_t, or ???

--
Hope this helps,
Steven

Jul 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3213
by: Chris Online | last post by:
Hi all, I'm using C++ Builder5. I want to get data from an edit-box and send it to a development kit. The dev-kit can only receive char and no char* here's a part of my code: char* Data_byte = 0x00; UCHAR outBuffer;
7
2411
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc... Whats the best way to read in each line and break it up into individual ints?
8
4769
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! -RB
3
3105
by: j.a. harriman | last post by:
Hi, On MSDN I know there is a JScript example (Upgrading Visual C++ Projects to Visual Studio .NET in Batch Mode) to upgrade VS6 C++ projects to .NET solutions. It converts the project files (*.dsp), but doesn't create the solution (*.sln) file. Is there an automated way to convert an existing VS6 C++ *.dsw to a .NET *.sln?
19
1294
by: silentlights | last post by:
Hi, Can you help me conver a char array into an integer. I am trying something like.. char carray; int numb; carray = 1; carray = 5; carray = 1;
1
2407
by: scott | last post by:
Hi all, trying to use base64. Ill get right to the problem. I am converting a string into base 64. No problem there. That base64 string can then be converted back to the orignal string. No problem there. The problem is, due to the nature of my program, i have to pad out the base64 with '=''. i know that a base64 string will some times use '=' it self to pad out the base64 string but i have to add extra to it. Now when i try to decode...
8
4193
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
5
1959
by: byte8bits | last post by:
Here's how I'm doing this right now, It's a bit slow. I've just got the code working. I was wondering if there is a more efficient way of doing this... simple example from interactive Python: .... char = unichr(int(h, 16)) .... word += char .... print char .... B r
2
6561
by: cablepuff | last post by:
template <typename ContainerType> ContainerType rsa_encrypt_list(const std::string&, const typename ContainerType::reference, const typename ContainerType::reference); const BigInteger e(boost::lexical_cast<BigInteger>(rsa_encrypts)); const BigInteger n(boost::lexical_cast<BigInteger>(rsa_encrypts)); std::string infile(rsa_encrypts); boost::scoped_ptr<boost::filesystem::ifstreamencrypt_input(new boost::filesystem::ifstream(infile));
2
5818
by: joe shoemaker | last post by:
I would like to convert url into md5 hash. My question is that md5 hash will create collision at 2^64. If you do long(value,16), where value is the md5 hash string, would value returned from long(value, 16) be unique as long as md5 hashed string is unique? when you move md5 hashed string to long, where will the collision occur, at anything hash = md5.new() hash.update("some_url_") value = hash.digest() value_in_int = long(value, 16)...
0
8694
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.