0

I have a string file, but the strings in it represent hexadecimal values. For example, I have this kind of string in my file:

1091    A3B7    56FF    ...

And I don't want to use them as string, but as hexadecimal values; and then convert the hexadecimal into an int.

For example:

1091(in string)---> 1091(in hexa)---> 4241 # The int value of 1091 in hexa

So I looked on the Internet. I tried a lot of different methods such as:

  1. Print a string as hex bytes?

  2. Convert hex string to int in Python

But nothing fit exactly with what I need, or simply does not work.

This is a part of my code:

t = False
i = 0
while t != True and h != True or i <=100: # My way to look each string of my file
    file = row[1]
    read_byte = file[i]

    if read_byte == 'V': #V is the letter in my file which says that it s the data that I want then there is 2 kinds of channel 01 and 02 that interest me

        i=i+1
        a=i+2
        read_bytechannel = file[i:a]  #read 2 strings because the channel can be '01' or '02'

        if read_bytechannel == '01':
            print(read_bytechannel)
            i=i+1
            a=i+4
            read_bytetemp = file[i:a] # Reading 4 strings because the value is the int value of the two hexa.
            realintvalue= # (?????????convert the read_bytetemp string value into an hexa value, then into an int from the hexa)

            tempfinal = realintvalue/100 # I have to divide by 100 the int value to get the real temperature
            t = True # This condition just tell me that I already know the temporary

    i = i+1

And this is the kind of file I want to read:

@
I01010100B00725030178
V01109103
I02020100B00725030148
V0215AA5C
$
@
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Max Taylor
  • 129
  • 3
  • 15
  • 1
    Your input is not hexadecimal and it does not look like the `1091 A3B7 56FF` you describe at the beginning. What is your real input? –  Apr 07 '15 at 15:11
  • My initial input are byte from a serial usb, that I put into an sql database, then now I am analysing the datum, so I have to read from the database. But what I showed you at the end of my question, was exactly what I get if I look the file created by the serial read of my script. – Max Taylor Apr 07 '15 at 15:21
  • Please provide a sample of your real input and describe the structure. –  Apr 07 '15 at 15:23

2 Answers2

1
>>> int('1091', 16)
4241

The second parameter to int is the base which to interpret the first parameter.

C.B.
  • 8,096
  • 5
  • 20
  • 34
1

You can try something like this, int with base 16:

>>> my_string = "1091 A3B7 56FF"
>>> map(lambda x:int(x,16), my_string.split())
[4241, 41911, 22271]

Or you can have both:

>>> map(lambda x:[x,int(x,16)], my_string.split())
[['1091', 4241], ['A3B7', 41911], ['56FF', 22271]]

If you are not comfortable with lambda and map, you can use list comprehension:

>>> [[x,int(x,16)] for x in my_string.split()]
[['1091', 4241], ['A3B7', 41911], ['56FF', 22271]]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • Thank you, but what if my strong is only 1091? because I tried you method, and it gives me: this as an answer and I would like 4241. Thank you again – Max Taylor Apr 07 '15 at 15:30
  • @MaxTaylor if you using python3, apply list, like this `list(map(lambda x:int(x,16), my_string.split()))` – Hackaholic Apr 07 '15 at 15:32