0

Say I have someString = "00". basically I want to convert someString to \x00 I tried multiple ways to achieve my goal, but couldn't find a successful one.

tried:

HexString = '\x'+someString

This method throws this error:

ValueError: invalid \x escape

Unless I do HexString = r'\x'+someString, but then HexString value is set to \\x00 which is not the same as I want.

I also tried using hex() function, which had few issues. But the big issue I had with it was that it returns 0x0. It expects int and etc...

Can anyone help me with converting a string("11") to \x11?

Abdul
  • 63
  • 1
  • 10
  • By `\x00` do you mean a string that is `"\\x00"` or an integer? – tdelaney Aug 02 '20 at 04:12
  • Does this anwer your question https://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python – Joe Ferndz Aug 02 '20 at 04:17
  • @tdelaney to hex, more precisely e.g `\x41` which is equal o `A` – Abdul Aug 02 '20 at 04:20
  • @JoeFerndz Unfotunetaly no, I want to convert a string to hex, not hex to int. E.g: `\x41` to string `A` in ascii table. – Abdul Aug 02 '20 at 04:21
  • So, in python terms, that would be "11'' to "A"? I'm still trying to figure out what `\x11` or `\x41` is. You can have an integer, a character in a string or a byte in a bytes array. – tdelaney Aug 02 '20 at 04:23
  • 2
    Maybe this? `bytes.fromhex(someString).decode('ascii')` – ddoGas Aug 02 '20 at 04:24

3 Answers3

4

If I am understanding you correctly, the actual goal is to take a string that contains a bunch of pairs of hex digits, and translate each pair of hex digits into the corresponding byte and have a result of type bytes.

In 3.x, this is built directly into the bytes type itself:

>>> bytes.fromhex('11abcdef')
b'\x11\xab\xcd\xef'

You can also instead use the standard library:

>>> import binascii
>>> binascii.unhexlify('11abcdef')
b'\x11\xab\xcd\xef'

You will not necessarily see a \x escape sequence for every byte value. This is normal and expected; it has to do with how the bytes object is represented as text for display purposes.

'\x'+someString

No approach of this general form can work, because it fundamentally misunderstands the problem. The output that you want is not a string, and a string literal like '\x00' does not have a backslash in it, nor a lowercase x - again, what you are seeing is how the string is represented as text, because not every character is printable.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
3

int lets you set the base. For base 16

>>> someString = "00"
>>> int(someString, 16)
0

Of course, 0 is kinda boring because it works for all bases.

If you wanted a byte in a bytes object, you could

>>> import struct
>>> struct.pack("b", int(someString, 16))
b'\x00'

If you want a string (and I'm switching to 0x41 here) you could

>>> chr(int("41", 16))
'A'
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Your method works if the hex value is smaller than 127, meaning if you have `someString = "aa"`, I get this error: `struct.error: byte format requires -128 <= number <= 127` – Abdul Aug 02 '20 at 04:38
  • @Abdul, glad that last one worked. `struct.pack` lets you build arbitrarily long byte streams based on a format specification. "b" is -127 to 127. "B" is 0-255. " – tdelaney Aug 02 '20 at 04:52
1

You can get ord of the character by using int, then convert it to a character. Then you can encode it to bytes object without any import.

>>> chr(int("11", 16))  # a character
'\x11'
>>> chr(int("11", 16)).encode()  # bytes object
b'\x11'
HK boy
  • 1,398
  • 11
  • 17
  • 25