473,893 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ 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 5956
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****@staco m-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.rp i.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
4554
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
6319
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 data before it is send to the browser. I'm trying to use REPLACE, but it's not finding a string that i know it's in the binary file. Using InstrB i've found that the search inside the binary data is being done in a Unicode format, but i don't
6
16027
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 vstr = rksub.GetValue(vs);
3
17933
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 (binary) or to A2 (hexadecimal) and vice versa. Thanks.
1
2759
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 follows, (Data Type) (Number of Elements) (Description) CHAR 8 File identifier, CHAR 8 Format identifier, CHAR 8 Program identifier,
2
4386
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. The binary data is how they send numbers (they call it "Big Endian"). I only know at run time whether a byte is going to be text or binary (one of the fields I decode tells me which the latter fields are). The code I have converts a single byte of...
6
4096
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 thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm, and here it starts getting interesting. I want to find an algorithm that satisfies these...
4
2253
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 this new user tries to login, a new hash will be created for the given password and compared to the stored hash. I guess this is quite common.
4
4406
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 conversion: string revLim = Convert.ToString(curveData, 2); // curveData = 0x14 // I want 00010100 // I am getting 10100
0
9987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
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
10929
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
9647
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
8025
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
7175
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.