how to convert from network to host byte order

E

Evan

Hello ~

I'm new with python, what my problem is, I have a binary file, I want
to read first 2 bytes and convert it to host byte order, then write it
to another file.

I try to use 'socket' and 'struct', but somehow i can not get it
working fine:

for example, totally I'm not sure if my steps is correct or not:
++++++++++++++++++++++++++++++++++++++++++++++++
'\x04\x00'
f.seek(0)
st=f.read(2)
e=open('test.bin','w+b')
e.write(socket.ntohs(struct.unpack('H',st[:2])[0]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only buffer, not int
+++++++++++++++++++++++++++++++++++++++++++++++++

It failed due to the parameter is 'int', not 'str' in write function.
but how can i do that?

Thanks,
Evan
 
J

Joe Riopel

Hello ~

I'm new with python,  what my problem is, I have a binary file, I want
to read first 2 bytes and convert it to host byte order, then write it
to another file.

Have you checked out socket.htons, socket.ntohs, etc ?
 
P

Philipp Hagemeister

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
inp='\x04\x00'
out = socket.ntohs(struct.unpack('H',inp[:2])[0]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only buffer, not int

Your approach is nearly right. First of all, you have to tell
struct.unpack it should unpack from network order ("!"):
1024

Then you want to repack it in host byte order. Use "=" for that.
out = struct.pack('=H', struct.unpack('!H', inp)[0])
out
'\x00\x04'

For more information, look for "Size and alignment" in
http://docs.python.org/library/struct.html.

Regards,

Philipp Hagemeister
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREKAAYFAkmv2JkACgkQ9eq1gvr7CFymKACghFXMZb9D6pkWZQdapvwTsKJ5
b0UAn0Uvbcguv/rdxjFKXhMQz22+Notn
=ZiKx
-----END PGP SIGNATURE-----
 
E

Evan

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
inp='\x04\x00'
out = socket.ntohs(struct.unpack('H',inp[:2])[0]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only buffer, not int

Your approach is nearly right. First of all, you have to tell
struct.unpack it should unpack from network order ("!"):
struct.unpack('!H', inp)[0]

1024

Then you want to repack it in host byte order. Use "=" for that.
out = struct.pack('=H', struct.unpack('!H', inp)[0])
out

'\x00\x04'

For more information, look for "Size and alignment" inhttp://docs.python.org/library/struct.html.

Regards,

Philipp Hagemeister
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEAREKAAYFAkmv2JkACgkQ9eq1gvr7CFymKACghFXMZb9D6pkWZQdapvwTsKJ5
b0UAn0Uvbcguv/rdxjFKXhMQz22+Notn
=ZiKx
-----END PGP SIGNATURE-----

That's good, thanks Philipp
 
M

Mark Tolonen

Evan said:
Hello ~

I'm new with python, what my problem is, I have a binary file, I want
to read first 2 bytes and convert it to host byte order, then write it
to another file.


There is a piece of information missing here. What is the byte order of the
original binary file?

I try to use 'socket' and 'struct', but somehow i can not get it
working fine:

for example, totally I'm not sure if my steps is correct or not:
++++++++++++++++++++++++++++++++++++++++++++++++
'\x04\x00'


This is either a little-endian 4, or a big-endian 1024 (0x400).

f.seek(0)
st=f.read(2)
e=open('test.bin','w+b')
e.write(socket.ntohs(struct.unpack('H',st[:2])[0]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument 1 must be string or read-only buffer, not int
+++++++++++++++++++++++++++++++++++++++++++++++++

It failed due to the parameter is 'int', not 'str' in write function.
but how can i do that?

socket.ntohs returns an integer. write takes a string. ntohs assumes the
original value was big-endian (network) order.

If the original binary file is little-endian, this works:

import struct
f=open('a.bin','rb')
data = struct.unpack('<H',f.read(2))[0]
f.close()
e=open('test.bin','wb')
e.write(struct.pack('H',data)) # default is host-order, could be big or
little.
e.close()

If the original binary file is big-endian, change the 3rd line:

data = struct.unpack('>H',f.read(2))[0]

-Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top