I currently get a string as a parameters in my method,
i would like to extract the char in the I index and get it’s hex value.
Currently i’m doing :
temp = string[i]
binascii.hexlify(temp);
but i get an error :
TypeError: 'str' does not support the buffer interface
Any ideas please ?
You need to encode the string to bytes:
You’ll need to pick a suitable encoding, one that can represent your text properly; I am presuming that your unicode characters fall in the 0-127 range here.
If you encode to a different encoding, the result will be a hex representation of that encoding. UTF-8 will use between 1 and 6 bytes per character for example.
Alternatively, you could use the
ord()function and format the result to hex:and it’ll work with any unicode character. It’ll use the Unicode code point for the hex representation, so between 1 and 4 bytes (the latter for
\Uabcdefghwide characters). Depending on your maximum character width, you may want to pad the bytes to prevent ambiguous code points; say you need to encode up to codepoint\uffffthen you’ll need to use 2 bytes for every codepoint, or 4 hex characters: