FenryrMKIII
FenryrMKIII

Reputation: 1198

Python : convert a float encoded as a byte string (from PyTorch) to an int

I have converted an output from PyTorch using .detach().numpy() which produces this kind of data :

b'0.06722715'

which is a byte type according to type() from Python. How can I convert this to an integer ?

Upvotes: 0

Views: 149

Answers (1)

Thaer A
Thaer A

Reputation: 2323

Try this (explaination in code comments). You can convert 0.06 to an integer but you'll get a zero. Did you mean float?

#byte
b = b'0.06722715'
# to string
s = b.decode()
# to float
f = float(s)
# to integer
i = int(f)
print("Float", f)
print("Integer", i)

or simply

be_float = float(b.decode())
print (be_float)

Upvotes: 2

Related Questions