Questions tagged [binascii]
50 questions
1
vote
1 answer
How do I know which endian to use in struct.unpack() when converting hexadecimal string to float?
I have data in form of hexadecimal string and I convert it to float as:
import struct, binascii
a = '0X437A1AF6'
x = struct.unpack('>f', binascii.unhexlify(str(a)[2:]))
print(x[0])
I get the right result but How do I prove that using big endian…

Jakub
- 11
- 1
1
vote
1 answer
I want to read the hexadecimal number of a binary file sequentially by 6 bytes
import binascii
import sys
from os.path import getsize
target = './'+sys.argv[1]
with open(target,'rb+') as f:
file_size = getsize(target)
string1 = f.read(6)
print("Size : %d"%file_size)
print (binascii.b2a_hex(string1))
I…

Sliced_ice
- 37
- 4
1
vote
1 answer
How to get the sha256 of a 'binary' array in Python
given the following Python code:
bits = [0]*256 # for simplicity (actually it's more like (0|1)^n)
binstr = "" # make string containing all 'bits'
for el in bits:
binstr += str(el)
how can I get the binary string of the sha256 of the bits/…

Root_DE
- 71
- 6
1
vote
1 answer
Calculating CRC32 checksum on string
I am in the process of bringing an API up from 2.7 to 3.8. It is built to communicate with an agent over a TCP socket. I am having an issue with a checksum not being calculated right. I get a message back from my agent that the Checksum is bad.
I…

mattmccj
- 11
- 2
1
vote
1 answer
What does binascii.hexlify(os.urandom(32)).decode() mean?
I'm trying to develop a function which would refresh token model in django rest framework.They seem to use binascii.hexlify(os.urandom(32)).decode() for generating unique tokens for every user.How does this line ensures that token generated by it…

Gagan Singh
- 200
- 1
- 13
1
vote
0 answers
why does my code give me different results than expected
I am working on a code to read through 2 hex files and write out the difference and convert (using binascii.unhexify()) it to decimal so i can view it and make changes.
I tried to convert it back to hex format with the reverse (binascii.hexify())…

delandy
- 11
- 3
1
vote
1 answer
Writing a binary file with lines more than 45 bytes: binascii.Error: At most 45 bytes at once
My goal is to read a binary file and convert it to text. My code is :
def binary_to_text(self,file_name):
open_file = open(file_name,"rb")
with open("Binary2Text.txt", "a") as the_file:
for line in open_file:
…

helloworld95
- 366
- 7
- 17
1
vote
1 answer
Python error TypeError: argument should be bytes, buffer or ASCII string, not 'NoneType'
I'm trying to create a tool to transfer binary files in python 3 and running into an odd issue on the server side. In the server code you will see I have one line commented out which prints the data received from the client to the screen.
With this…

user4573
- 21
- 1
- 1
- 4
1
vote
1 answer
Python binascii.unhexlify is converting linefeed characters. Can I prevent this?
I have an string of pairs of characters representing hex numbers, (e.g. 0f means decimal 15).
I need this in binary format, so I called
binArray = binascii.unhexlify(finalString)
I have "bash on Ubuntu on Windows" installed, and when I perform…

Randy Leberknight
- 1,363
- 9
- 17
0
votes
3 answers
Python3 bytes to string with real hex bytes
I've seen a lot of posts about this subject but I haven't found the solution I'm looking for (despite lot of attempts ...).
In a nutshell, when migrating one of my libraries from Python2 to 3, whose main data model is based on hex strings like this…

Robby
- 1
- 1
0
votes
1 answer
python: hex to bin with binascii.a2b_hex results in binascii.Error: Odd-length string
i am trying to convert a hex string to bin using binascii.a2b_hex but i get binascii.Error: Odd-length string only with some strings, not everytime.
for example this is the string throwing the error:…

91DarioDev
- 1,612
- 1
- 14
- 31
0
votes
3 answers
How can i convert my hex values to binary in Python? binascii.a2b_hex error
Hello when I use binascii.a2b_hex, error is occured.
There is no error message but the return value is wrong.
Here is the data and the resule.
Input : FEB10AA86764FFFF
Output : b'\xfe\xb1\n\xa8gd\xff\xff'
import binascii
ta['data'] =…

WonSeok JIN
- 13
- 1
0
votes
1 answer
Mac address conversion to bytes
I am trying to convert a mac address in hex like this: 00453645f200
to a Mac address in bytes like this:
x00\x45\x36\x45\xf2\x00
But if I use binascii.unhexlify, the result is like this:
b'\x00E6E\xf2\x00'. How can I get the result I want?

Ti a
- 1
0
votes
2 answers
Pack into c types and obtain the binary value back
I'm using the following code to pack an integer into an unsigned short as follows,
raw_data = 40
# Pack into little endian
data_packed = struct.pack('

user1241241
- 664
- 5
- 20
0
votes
0 answers
Python: binascii.Error: Invalid base64-encoded string
I am processing the email files from Amazon SES. The email contains a pdf file encoded with base64.
Content-Type: application/octet-stream; name="IN081AKC.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;…

Sushma Yadav
- 13
- 3