MrZH6
MrZH6

Reputation: 237

Python Converting hex string to decimal values

I am given strings structured as such: "\x0C\x00Maximilianus\xf4\x01" and I would like to dynamically extract first two and last two bytes and convert them to decimals. The encoding for this should be UTF-8 little-endian unsigned.

"\x0C\x00" equals 12

"\xf4\x01" equals 500

I am not able to find any function that would be able to do that. Also replacing "\x" in the string doesn't work as I cannot manipulate with escape characters.

Any thoughts?

Upvotes: 0

Views: 1066

Answers (2)

MrZH6
MrZH6

Reputation: 237

So here is my final solution with the help from furas:

data = "\x0C\x00Maximilianus\xf4\x01".encode('latin1')
name_len = int.from_bytes(data[:2],byteorder="little")
ending = int.from_bytes(data[-2:],byteorder="little")

print(name_len) # --> 12
print(ending) # --> 500

Upvotes: 0

furas
furas

Reputation: 143098

You can use struct to get numbers.

Using table Format Characters you can see you need "h" to convert 2-bytes integer.
You can eventually use "<h" to make sure it will use little-endian

import struct

# convert to bytes
data = "\x0C\x00Maximilianus\xf4\x01".encode('latin1')

# get short integer
number = struct.unpack('<h', data[:2])[0]
print('number:', number)

# skip number
data = data[2:]

# get string
#text = struct.unpack(f'{number}s', data[:number])[0] # use `number` to create `"12s"`
#print('text:', text.decode())
print('text:', data[:number].decode())

# skip string
data = data[number:]

# get short integer
number = struct.unpack('<h', data[:2])[0]
print('number:', number)

BTW: it looks similar to MessagePack so maybe there is special module for this but I don't know it.

Upvotes: 1

Related Questions