473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write an hexadecimal file

Hi

I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one, and
that's done.

That number is the one I'm gonna write in the hex file... can anybody
help me 'cause i don't know how python write an hex file!

Thanks!

--
Atentamente,

Cesar Andres Roldan Garcia
Presidente Comunidad Académica Microsoft Javeriana
Cali - Colombia
Jul 18 '05 #1
3 15422
There is not such thing as a hexadecimal file. Files are either
binary or text. Hexadecimal is a representation of binary data
where 4 bits are displayed as a single hex digit (0-F) as
shorthand (because 1111000001111000111 is just too difficult to
deal with).

To write binary data to a file in Python you open the file with
"wb" mode.

Example:

fp=open('myfile.dat', 'wb')
fp.write(bytes)
fp.close()

From your post I cannot tell anything more about what you are
actually doing, so I hope this helps.

Larry Bates
Cesar Andres Roldan Garcia wrote:
Hi

I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one, and
that's done.

That number is the one I'm gonna write in the hex file... can anybody
help me 'cause i don't know how python write an hex file!

Thanks!

Jul 18 '05 #2
[Cesar Andres Roldan Garcia]
I'm trying to write an hexadecimal file... I mean not a text plain...
I have to convert a float decimal number in float hexadecimal one,
and that's done.


The struct module provides a portable way to convert a float to and from a
sequence of bytes.

The binascii modules provides tools for converting a sequence of bytes to and
from a representation as a hex string.
import struct, binascii
binascii.hexlify(struct.pack('>f', 3.1415926535)) '40490fdb' struct.unpack('>f', binascii.unhexlify(_))[0]

3.1415927410125732

Writing to a file is accomplished with the open() function and the file.write()
method:

f = open('mydata.hex', 'w')
f.write('40490fdb')
f.close()

Raymond Hettinger
Jul 18 '05 #3
rbt
Larry Bates wrote:
There is not such thing as a hexadecimal file.


Right, 300 is 300 whether you choose to represent it in decimal, binary,
hex, etc... it's still only 300 of something ;)
Jul 18 '05 #4

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

Similar topics

0
1819
by: thomasp | last post by:
I have not had any luck with this on other groups so I am posting it here. I created a .rtf file using MS Word that included text and graphics(.png file) . I then viewed this file with notepad. ...
4
11106
by: Tom Van Ginneken | last post by:
Hi, I need to write binary data to a serial port. I am using this function: #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); I am able to write a alpha-numeric...
5
7886
by: Damon | last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm deseralizing XML from a 3rd party XML gateway. How do I get rid of these hexadecimal values before I deserialize? Cheers...
3
8516
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert...
8
18636
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
7
19191
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
13
7243
by: Angus | last post by:
Hello I have a load of bytes like this: unsigned char mybyte = {0xa1, 0x14, 0x02, 0x01, 0x01, 0x02, 0x01, 0x0a, 0x30, 0x0c, 0x80, 0x03, 0x32, 0x30, 0x31, 0x62, 0x05, 0x80, 0x03, 0x32, 0x30,...
14
2906
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I...
4
5312
by: Thomi Aurel RUAG A | last post by:
Hy Mike Thanks for your links, unfortunately they weren't very usefull for my specific problem. Hy Grant Edwards Thanks for your hints. A simplified test programm to compare the function for...
0
7207
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,...
0
7095
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
7294
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
7015
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
7470
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
4693
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
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
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 ...
1
749
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.