# [Tutor] Another parsing question

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Mar 16 13:27:25 EST 2004

```Vicki,

> I have inherited a project which writes some data to a file
> in hexadecimal format as 0x11, 0x4, etc. The hex values are
> pipe-delimited as so: 0x11|0x4|0x14

Don't take this the wrong way, but it seems you really need to go
back to basics on how information is stored and represented in
computer systems. Most of your questions seem to be based on
confusion between strings and binary formats.

> hex, but when I send the data that I have parsed out, I get
> it sending the 0, the x, the 1, and another 1 instead of the
> hex value for 0x11.

That's because you have parsed a string - ie a series of characters.
You want to send a series of bytes. You need to convert the 4
byte character strings into single byte numbers.
Try using the int() operaion:

>>> int('12')
12
>>> int('0x12',16)
18
>>> print "As hex: %0X" % int('0x12',16)
As hex: 12

Notice the hex is purely a representation thing. There is no
real difference between hex 0x12 and decimal 18 (or even octal 22!)
They are just different string representations of the same
underlying binary number.

Now you can send that binary number down the pipe:

if len(line)>1:
tokens=line.split("|")
i=0   # I have no idea what you need the i for???
for piece in tokens:
port.write(int(piece,16)) # convert to number
time.sleep(S_INTERVAL)
i = i + 1  # nor why you increment it here

HTH,

Alan G.

```