Posts: 27
Threads: 7
Joined: Aug 2020
Hi, I have been given a file and was told it was a binary file.
In Python I can open the file and read what I think is for four bytes (4).
byte = f.read(4)
print(byte)
In the shell I get
b'B\xda\xed\xc5' for example
I would like to get this in the form of something like 00001111 10100000 10101010 10100000
Without the spaces or able to remove the spaces.
So that I can convert it into int32.
(I was hoping to put up a screen capture, however I cannot see a way to do so).
Hope some one can help, there may be a better way to go about this.
Thanks,
Posts: 4,804
Threads: 77
Joined: Jan 2018
Aug-28-2020, 11:11 AM
(This post was last modified: Aug-28-2020, 11:11 AM by Gribouillis.)
The bytes type is already an array of integers in the range(0, 256).
>>> x = b'B\xda\xed\xc5'
>>> list(x)
[66, 218, 237, 197] For the conversion to 32 bits integer, you could have a look into the struct module
>>> import struct
>>> x = b'B\xda\xed\xc5'
>>> struct.unpack('i', x)
(-974267838,)
>>> struct.unpack('<i', x)
(-974267838,)
>>> struct.unpack('>i', x)
(1121643973,)
Posts: 27
Threads: 7
Joined: Aug 2020
(Aug-28-2020, 11:11 AM)Gribouillis Wrote: The bytes type is already an array of integers in the range(0, 256).
>>> x = b'B\xda\xed\xc5'
>>> list(x)
[66, 218, 237, 197] For the conversion to 32 bits integer, you could have a look into the struct module
>>> import struct
>>> x = b'B\xda\xed\xc5'
>>> struct.unpack('i', x)
(-974267838,)
>>> struct.unpack('<i', x)
(-974267838,)
>>> struct.unpack('>i', x)
(1121643973,)
Thanks Gribouillis, I have changed the code a little to get unsigned values.
temp = struct.unpack('I', byte)
print(temp)
I am not getting the value's expected.
Is this because of LSB and / or MSB.
I don't understand how to implement in the unpack function.
Thanks,
Posts: 1,583
Threads: 3
Joined: Mar 2020
What 32 bit value do you expect for your input data (b'B\xda\xed\xc5')?
Posts: 27
Threads: 7
Joined: Aug 2020
Hi,
for 01011111 00011000 01100101 00111010, I would expect 1595434298
(Do I need to get the spaces out)
Thanks,
Posts: 1,583
Threads: 3
Joined: Mar 2020
I thought you were starting with b'B\xda\xed\xc5' . That doesn't yield those binary strings.
If you're starting with 01011111 00011000 01100101 00111010 , then yes you can just smash them together.
>>> binary_string = "01011111 00011000 01100101 00111010"
>>> int(binary_string.replace(" ", ""), 2)
1595434298
Posts: 27
Threads: 7
Joined: Aug 2020
How do I get "byte = f.read(4)" into "binary_string"?
Thanks,
Posts: 1,583
Threads: 3
Joined: Mar 2020
You said you wanted to convert from 01011111...., so I assumed you already had that value.
If instead you start from the f.read(4), then you go as above and either use unpack as Gribouillis showed or do the math yourself by adding and multiplying.
>>> f = open("/tmp/num", "rb")
>>> byte = f.read(4)
>>> print(byte)
b'B\xda\xed\xc5'
>>> struct.unpack('>i', byte)
(1121643973,)
>>> ((byte[0] * 256 + byte[1]) * 256 + byte[2]) * 256 + byte[3]
1121643973
Posts: 27
Threads: 7
Joined: Aug 2020
Aug-31-2020, 04:46 AM
(This post was last modified: Aug-31-2020, 04:47 AM by Aussie.)
Hi, how do I convert byte into a binary string or int32 of same format?
01011111000110000110010100111010
Thanks,
Posts: 27
Threads: 7
Joined: Aug 2020
Hi, the reason why is that I want to use
Epoch
Time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(int32))
Thanks,
|