0

My question is:

  • how can we convert the bytes to int64 in python

in C# we could use BitConverter.ToInt64()for transfer the bytes to int64. but I didn't find similar function in the python. how can I do it in the python. I just find the int.from_bytes().

input: System.Byte[], \x12\x77\x2b\xca\x9b\x62\xa2\x72\x9e\xc8\xb7\xa7\x82\xd8\x4c\xba\xcb\x41\x78\x4c\x5a\x72\xdd\xf6

output: 4666902099556679087

kailing
  • 13
  • 5
  • 1
    What result do you need exactly? "int64" is not a Python type, perhaps, are you working with numpy? – juanpa.arrivillaga Jan 20 '21 at 01:13
  • Could you please provide sample input and expected output ? – fountainhead Jan 20 '21 at 01:30
  • Unlike C# where you have different `int` types like `short`, `int`, `long` (64bit int) - there's just one `int` type that would store a 64bit value on a 64bit machine or 32bit value on a 32bit machine. Just use the function you've already found. – kidroca Jan 20 '21 at 17:25
  • That's a lot of bytes you've got there, more than what would fit in a 64-bit integer. What's the relationship between that byte sequence and 4666902099556679087 supposed to be? – user2357112 Jan 21 '21 at 00:59
  • if i use bitcoverter.toint64(input), then i get the output 4666902099556679087 – kailing Jan 21 '21 at 06:35

1 Answers1

0

In python, there is only int and no int32 and int64. You can easily convert bytes string to int (supposing you are using only builtin types and not e.g. numpy):

bytesstr = bytes("123") # the bytes string
numstr = bytesstr.decode() # convert bytes string to normal string
num = int(numstr) # convert normal string to number
TheEagle
  • 5,808
  • 3
  • 11
  • 39