15

I am having some difficulty changing a hex to an int/char (char preferably). Via the website; http://home2.paulschou.net/tools/xlate/ I enter the hex of C0A80026 into the hex box, in the DEC / CHAR box it correctly outputs the IP I expected it to contain.

This data is being pulled from an external database and I am not aware how it is being saved so all I have to work with is the hex string itself.

I have tried using the binascii.unhexlify function to see if I could decode it but I fear that I may not have a great enough understanding of hex to appreciate what I am doing.

Attemping to print just using an int() cast also has not produced the required results. I need some way to convert from that hex string (or one similar) to the original IP.

UPDATE: For anyone who comes across this in the future I modified the below answer slightly to provide an exact printout as an IP by using;

dec_output = str(int(hex_input[0:2], 16)) + "." +  str(int(hex_input[2:4], 16)) + "." + str(int(hex_input[4:6], 16)) + "." + str(int(hex_input[6:8], 16))
Draineh
  • 599
  • 3
  • 11
  • 28
  • possible duplicate of [Convert hex string to int in Python](http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python) – S.Lott Sep 29 '11 at 09:57
  • That solution doesn't work with my problem – Draineh Sep 29 '11 at 10:14
  • Please explain exactly how the duplication question is not a duplicate. Details matter. "doesn't work with my problem" is too vague to mean anything. – S.Lott Sep 29 '11 at 10:46
  • Sorry, I assumed you had compared them already. My problem is related to decoding hex into an IP address which the other question does not cover. Also although I do not know a great deal about hex or python there are no related questions which suggests that our similarity ends at trying to decode hex but both trying to reach two different ends – Draineh Sep 29 '11 at 11:12
  • It helps to avoid all assumptions. I still don't understand why the supplied question is not the answer to your question because (1) I don't understand the nuances of your question and (2) I don't understand the gaps in your knowledge. Rather than assume, please **update** your question to detail -- specifically -- why a widely-accepted existing answer isn't appropriate for this. Details matter. And **updates** to the question are better than comments. – S.Lott Sep 29 '11 at 12:57

6 Answers6

12

If you want to get 4 separate numbers from this, then treat it as 4 separate numbers. You don't need binascii.

hex_input  = 'C0A80026'
dec_output = [
    int(hex_input[0:2], 16), int(hex_input[2:4], 16),
    int(hex_input[4:6], 16), int(hex_input[6:8], 16),
]
print dec_output # [192, 168, 0, 38]

This can be generalised, but I'll leave it as an exercise for you.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
9

A simple way

>>> s = 'C0A80026'
>>> map(ord, s.decode('hex'))
[192, 168, 0, 38]
>>> 

if you prefer list comprehensions

>>> [ord(c) for c in s.decode('hex')]
[192, 168, 0, 38]
>>> 
Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
  • Nice one. I'd _personally_ write it as a list comprehension, but that is purely taste. I like the use of `decode` and `ord`. – neil Sep 29 '11 at 09:46
5

You might also need the chr function:

chr(65) => 'A'
JC Plessis
  • 683
  • 3
  • 7
  • chr accepts only integers the question states clearly that the user needs to convert a hex in an int/char (not an int to char) and chr accepts only integers so this is not a solution. – jnhghy - Alexandru Jantea Feb 05 '15 at 14:56
3

For converting hex-string to human readable string you can escape every HEX character like this:

>>> '\x68\x65\x6c\x6c\x6f'
'hello'

from string you can easily loop to INT list:

>>> hextoint = [ord(c) for c in '\x68\x65\x6c\x6c\x6f']
>>> _
[104, 101, 108, 108, 111]

Your example:

>>> [ord(c) for c in '\xC0\xA8\x00\x26']
[192, 168, 0, 38]
MaKiPL
  • 1,200
  • 8
  • 15
3
>>> htext='C0A80026'
>>> [int(htext[i:i+2],16) for i in range(0,len(htext),2)]
# [192, 168, 0, 38]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

I hope it's what you expect:

hex_val = 0x42424242     # hexadecimal value
int_val = int(hex_val)   # integer value
str_val = str(int_val)   # string representation of integer value
bluish
  • 26,356
  • 27
  • 122
  • 180
Manuel Leduc
  • 1,849
  • 3
  • 23
  • 39