0

I tried below methods, but no luck.

method 1:

var contains the hex value

bytes.fromhex(var).decode('ascii')
Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeDecodeError: 'ascii' codec can't decode byte 0xdb in position 0: ordinal not in range(128)

method 2:

codecs.decode(var,"hex")

This is returning me in bytes, not in ASCII.

Can someone help on this conversion?

MaximTitarenko
  • 886
  • 4
  • 8
apan
  • 353
  • 3
  • 11
  • In the comment to one of the answers you pasted the actual value of `var`. This hex string can't be ASCII-encoded as it contains hex values out of ASCII range. The error message is valid. – ElmoVanKielmo Oct 18 '17 at 12:14
  • but with python 2.7.3 i am able to convert it with this method (var.decode("hex")) – apan Oct 18 '17 at 12:17
  • with python 2.7.3 this is the result >>> var = var.decode('hex') >>> type(var) I cannot paste the var ouput as its too long,but it works. – apan Oct 18 '17 at 12:21
  • @CoolNetworking, so do you use P 2.7 or 3.6? There is a huge difference in working with strings – MaximTitarenko Oct 18 '17 at 12:38
  • My question clearly states it "Not able to convert HEX to ASCII in python 3.6.3".With py 2.7 i am able to get it working – apan Oct 18 '17 at 12:39

5 Answers5

1

Did you try:

chr(var)

This should give you the character for an ASCII code.

Andreas
  • 221
  • 1
  • 11
  • how will that help as var contains hex value? This is my hex value.'DB91132598CCBF76AE668B4B085176BB192775221FDBB50FCE1C3927C077EAF1E3DAC4C8A8E8028C3F7295EF8157CED597A36EF1C3BFA451477BFF32EEB1806C2CC0442585197A2BCD2C4392147AADB93066D0BA5AB6DBFD3F6FCEB7073AE61A5D4AA8ABCBCFF4EEBD1A1655689BFD7EAB82D77BF7224F75FBCD323C9B9FA9C0B7D799180878A81D5D07CF05BE39EEF989B2FC3077997D2C2F1162E5B47D99E4B415BC8CE5C755476931BD8ED14B5FCEC1C8C654515946AB7860BBBEE7DCCFDAA7AA410FF65352B153B58728D57814C610F825286D830C308429BDCF1167887B125EFB2FA34B3DEACD329F57674C071BEF6C9CEAC0C7FABB587A1D6F8B4D0B53' – apan Oct 18 '17 at 11:55
  • In that case, var is a string with a hex coded value? You could try to get integer values with int(var, 16), but you will need to split var into tuples of two characters. – Andreas Oct 18 '17 at 11:56
  • this is what var contains 'DB91132598CCBF76AE668B4B085176BB192775221FDBB50FCE1C3‌​927C077EAF1E3DAC4C8A‌​8E8028C3F7295EF8157C‌​ED597A36EF1C3BFA4514‌​77BFF32EEB1806C2CC04‌​42585197A2BCD2C43921‌​47AADB93066D0BA5AB6D‌​BFD3F6FCEB7073AE61A5‌​D4AA8ABCBCFF4EEBD1A1‌​655689BFD7EAB82D77BF‌​7224F75FBCD323C9B9FA‌​9C0B7D799180878A81D5‌​D07CF05BE39EEF989B2F‌​C3077997D2C2F1162E5B‌​47D99E4B415BC8CE5C75‌​5476931BD8ED14B5FCEC‌​1C8C654515946AB7860B‌​BBEE7DCCFDAA7AA410FF‌​65352B153B58728D5781‌​4C610F825286D830C308‌​429BDCF1167887B125EF‌​B2FA34B3DEACD329F576‌​74C071BEF6C9CEAC0C7F‌​ABB587A1D6F8B4D0B53' – apan Oct 18 '17 at 11:57
  • 1
    @CoolNetworking add this and the desired output into your question – vaultah Oct 18 '17 at 11:59
1

As it follows from the question I assume using Python 3x.

The reason of your error is the fact, that you try to decode with ASCII the byte '0xdb' which has the value above 127.
You just can't do that - there is no such a byte value in ASCII encoding.

Your options are:

1. Ignore decode errors:

>>> u = 'DB91132598CC' # unicode
>>> b = codecs.decode(u,"hex") # bytes
>>> b
b'\xdb\x91\x13%\x98\xcc'
>>> result = b.decode("ascii", errors="ignore") # unicode
>>> result
'\x13%'

2. Use different encoding:

>>> result = b.decode("cp1252") # for example 
>>> result
'Û‘\x13%˜Ì'

If you want only ASCII chars in the result use option #1.

MaximTitarenko
  • 886
  • 4
  • 8
0
>>> var = int('7A', 16) #var is an integer now
>>> chr(var) #int value to char
'z'

This solution is only for one character. You have to split the string and convert all hex values seperated. Look here how to split it.

micharaze
  • 957
  • 8
  • 25
0

have you tried

 codecs.decode(codecs.decode(var,'hex'),'ascii')
umläute
  • 28,885
  • 9
  • 68
  • 122
  • yes but it gives me below error >>> codecs.decode(codecs.decode(var,'hex'),'ascii') Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xdb in position 0: ordinal not in range(128) – apan Oct 18 '17 at 12:03
0

The following code:

codecs.decode(var,"hex")

in Python 2.7 results with str:

'\xdb\x91\x13%\x98\xcc\xbfv\xaef\x8bK\x08Qv\xbb\x19\'u"\x1f\xdb\xb5\x0f\xce\x1c9\'\xc0w\xea\xf1\xe3\xda\xc4\xc8\xa8\xe8\x02\x8c?r\x95\xef\x81W\xce\xd5\x97\xa3n\xf1\xc3\xbf\xa4QG{\xff2\xee\xb1\x80l,\xc0D%\x85\x19z+\xcd,C\x92\x14z\xad\xb90f\xd0\xbaZ\xb6\xdb\xfd?o\xce\xb7\x07:\xe6\x1a]J\xa8\xab\xcb\xcf\xf4\xee\xbd\x1a\x16Uh\x9b\xfd~\xab\x82\xd7{\xf7"Ou\xfb\xcd2<\x9b\x9f\xa9\xc0\xb7\xd7\x99\x18\x08x\xa8\x1d]\x07\xcf\x05\xbe9\xee\xf9\x89\xb2\xfc0w\x99},/\x11b\xe5\xb4}\x99\xe4\xb4\x15\xbc\x8c\xe5\xc7UGi1\xbd\x8e\xd1K_\xce\xc1\xc8\xc6TQYF\xabx`\xbb\xbe\xe7\xdc\xcf\xda\xa7\xaaA\x0f\xf6SR\xb1S\xb5\x87(\xd5x\x14\xc6\x10\xf8%(m\x83\x0c0\x84)\xbd\xcf\x11g\x88{\x12^\xfb/\xa3K=\xea\xcd2\x9fWgL\x07\x1b\xefl\x9c\xea\xc0\xc7\xfa\xbbXz\x1do\x8bM\x0bS'

and in Python 3.6 bytes:

b'\xdb\x91\x13%\x98\xcc\xbfv\xaef\x8bK\x08Qv\xbb\x19\'u"\x1f\xdb\xb5\x0f\xce\x1c9\'\xc0w\xea\xf1\xe3\xda\xc4\xc8\xa8\xe8\x02\x8c?r\x95\xef\x81W\xce\xd5\x97\xa3n\xf1\xc3\xbf\xa4QG{\xff2\xee\xb1\x80l,\xc0D%\x85\x19z+\xcd,C\x92\x14z\xad\xb90f\xd0\xbaZ\xb6\xdb\xfd?o\xce\xb7\x07:\xe6\x1a]J\xa8\xab\xcb\xcf\xf4\xee\xbd\x1a\x16Uh\x9b\xfd~\xab\x82\xd7{\xf7"Ou\xfb\xcd2<\x9b\x9f\xa9\xc0\xb7\xd7\x99\x18\x08x\xa8\x1d]\x07\xcf\x05\xbe9\xee\xf9\x89\xb2\xfc0w\x99},/\x11b\xe5\xb4}\x99\xe4\xb4\x15\xbc\x8c\xe5\xc7UGi1\xbd\x8e\xd1K_\xce\xc1\xc8\xc6TQYF\xabx`\xbb\xbe\xe7\xdc\xcf\xda\xa7\xaaA\x0f\xf6SR\xb1S\xb5\x87(\xd5x\x14\xc6\x10\xf8%(m\x83\x0c0\x84)\xbd\xcf\x11g\x88{\x12^\xfb/\xa3K=\xea\xcd2\x9fWgL\x07\x1b\xefl\x9c\xea\xc0\xc7\xfa\xbbXz\x1do\x8bM\x0bS'

The reason is that in Python 2.7

str == bytes #True

while in Python 3.6

str == bytes #False

Python 2 strings are byte strings, while Python 3 strings are unicode strings. Both results are actually the same, but byte string in Python 3 is of type bytes not str and literal representation is prefixed with b.
This has nothing to do with ASCII encoding as none of the output variables (regardless of Python version) is ASCII encoded.
Moreover on Python 2.7 you will also get this error:

codecs.decode(var, 'hex').decode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xdb in position 0: ordinal not in range(128)

I actually pasted it from Python 2.7 interpreter but you can check yourself.
Your output string can't be decoded with ascii codec in any version of Python because it's simply not ascii encoded string in any case.

ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
  • Thanks for your reply.Actually, I want an equivalent of this decoding method (var.decode("hex")) in 3.6 version which works fine in 2.7 and gives me the expected result. – apan Oct 18 '17 at 16:46
  • @CoolNetworking the result is the same. Bytestring. Only notation is different since Python 3 has Unicode string as `str` type. – ElmoVanKielmo Oct 19 '17 at 01:17