473,464 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

data conversion question (binary string to 'real string')

Hallo all there,

maby I don't see the forest because of all that trees, but :

from struct import *
# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'
# to "3F000000" ?????
Thanks a lot for any help

Jul 18 '05 #1
5 5932
Manish Jethani wrote:
Alexander Eisenhuth wrote:

maby I don't see the forest because of all that trees, but :

Maybe I don't understand your question...

from struct import *
# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'
# to "3F000000" ?????

???????????????
What is your question?

-Manish

Sorry ...

This two lines converts 0.5 to a string in binary
representation
from struct import *
bin_str = pack('!f', 0.5)


now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??

Thaks
Alexander

Jul 18 '05 #2
Manish Jethani wrote:
Alexander Eisenhuth wrote:

maby I don't see the forest because of all that trees, but :

Maybe I don't understand your question...

from struct import *
# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'
# to "3F000000" ?????

???????????????
What is your question?

-Manish

Sorry ...

This two lines converts 0.5 to a string in binary
representation
from struct import *
bin_str = pack('!f', 0.5)


now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??

Thaks
Alexander
Jul 18 '05 #3
Alexander Eisenhuth wrote:
This two lines converts 0.5 to a string in binary
representation
>>>from struct import *
from binascii import hexlify
>>>bin_str = pack('!f', 0.5)


now bin_str is '?\x00\x00\x00', wich is the representation
of 0.5 in the memory. I need now to convert this binary
string to "3F000000", where '3F' is the hex value of the '?'
in ascii - table.

Any ideas ??


foo = hexlify(bin_str)
print foo

-Manish

PS: You posted the same message thrice, by mistake.

--
Manish Jethani (manish.j at gmx.net)
phone (work) +91-80-51073488

Jul 18 '05 #4
On Fri, 25 Jul 2003 14:57:57 +0200, Alexander Eisenhuth <st****@stacom-software.de> wrote:
Hallo all there,

maby I don't see the forest because of all that trees, but :

from struct import *
# how can I convert
f = float(0.5)

# with
bin_str = pack('!f', 0.5)

# now bin_str is '?\x00\x00\x00'
# to "3F000000" ?????

That leading '?' is the character representation of a string byte whose ordinal value
is 3F hex, followed by three characters represented by \x00 (whose ordinal
values are zero). I.e., your binary bytes are being represented as characters in a string,
and when it's shown interactively you see the values as a sequence of character glyphs
(or \x.. hex escape representations if no glyphs are available), which is the normal way
to display a string.

If you want the *hex string* representation of that string-represented byte sequence, you can
convert each byte-as-character first to its ordinal value and then the ordinal value to a
2-char hex string representation, and then join those all into a single string,
e.g.,, showing the character transformations:
[c for c in struct.pack('!f', 0.5)] ['?', '\x00', '\x00', '\x00'] [ord(c) for c in struct.pack('!f', 0.5)] [63, 0, 0, 0] ['%02X'%ord(c) for c in struct.pack('!f', 0.5)] ['3F', '00', '00', '00']

And finally, all you need is
''.join(['%02X'%ord(c) for c in struct.pack('!f', 0.5)])

'3F000000'

Now you have a hex string representation of the network-ordered (big-endian-ordered)
bytes of a single precision (32-bit) float.

Regards,
Bengt Richter
Jul 18 '05 #5
FWIW here's an interesting way to get at the bit:
if 13 is the number you want to represent in binary and you want 6 bits:
[13>>x & 1 for x in range(6,-1,-1)]

[0, 0, 0, 1, 1, 0, 1]

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.500 / Virus Database: 298 - Release Date: 7/10/2003

Jul 18 '05 #6

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

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
1
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary...
6
by: John Hoffman | last post by:
Reading registry: .... RegistryKey rksub = rkey.OpenSubKey(s); String valstr = rksub.GetValueNames(); foreach (String vs in valstr) { String vstr = rksub.GetValue(vs).ToString(); OR String...
3
by: Amjad | last post by:
Hi, Are there any built-in methods in VB.NET that convert one number in one format to its equivalent in another format? For example I want to convert the number 162 (decimal) to 10100010...
1
by: tony.fountaine | last post by:
I am working on a project to read a Bosch Measurement Data File (MDF). The file contains a number of blocks that can be read from the file using a baisc structure. For example the ID BLOCK is as...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
6
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my...
4
by: vcnewbie | last post by:
Hi I'm maintaining a VisualC++ project to increase its security regarding stored passwords. I thought about using SHA256Managed to create a hash for the password when creating a user and when...
4
by: dondigitech | last post by:
I want to convert hex to binary without losing bits. I want to preserve the 8-bits because I ultimately need a 24-bit string to grab information from. I am just using this line of code for the...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.