annonymous
annonymous

Reputation: 131

How to convert a string to array and convert each of the hex value in array to decimal in python?

I want the following steps to be implemented in python

1) string 7f33117cf266a525

2) uppercase 7F33117CF266A525

3) Put it in an array [7F,33,11,7C,F2,66,A5,25]

4) convert it to binary[127,51,17,124,242,102,165,37]

and vice-versa

1) binary[127,51,17,124,242,102,165,37]

2) convert to hex [7F,33,11,7C,F2,66,A5,25]

3) 7F33117CF266A525

4) 7f33117cf266a525

string="7f33117cf266a525"
print(string.upper())
T=list(string)
T

gives an output ['7', 'F', '3', '3', '1', '1', '7', 'C', 'F', '2', '6', '6', 'A', '5', '2', '5'] how to seperate two characters with comma?

Upvotes: 1

Views: 141

Answers (5)

blhsing
blhsing

Reputation: 106936

If you're using Python 3.5+ you can use the bytes.fromhex method to convert hex string to bytes, and use the list constructor to convert bytes into a list of integers:

>>> list(bytes.fromhex('7f33117cf266a525'))
[127, 51, 17, 124, 242, 102, 165, 37]

And you can use the bytes constructor to convert a list of integers to bytes, and use the bytes.hex method to convert bytes to hex string:

>>> bytes([127, 51, 17, 124, 242, 102, 165, 37]).hex()
'7f33117cf266a525'

Upvotes: 2

han solo
han solo

Reputation: 6600

You shouldn't really split it unless, you know the data,

>>> string
'7f33117cf266a525'
>>> string.upper()
'7F33117CF266A525'
>>> [ord(x) for x in string.decode('hex')]
[127, 51, 17, 124, 242, 102, 165, 37]
>>> [format(ord(x), 'x') for x in string.decode('hex')]
['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']

Upvotes: 2

Anurag A S
Anurag A S

Reputation: 731

This code might work.

def split_by_n(seq, n):
    while seq:
        yield seq[:n]
        seq = seq[n:]


string = input('enter string:') #enter input string
uppercase = string.upper()   #convert to upper case
split = (list(split_by_n(uppercase, 2)))  #split it by 2 characters
converted = [int(i, 16) for i in split]  #convert the base to bin/decimal
print(converted) #display the output


converted_back = [hex(i)[2:] for i in converted]    #convert it back to hex
back_to_string = "".join(converted_back)     #join them to get string
print(back_to_string) #print the output

Upvotes: 1

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6935

Try this to separate every two characters in the string :

T = [string[i:i+2] for i in range(0, len(string), 2)]
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']

However, if you have odd number of characters in string and want to get a list of every two characters starting from first, then try this :

T = list(map(''.join, zip(*[iter(string)]*2)))
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']

Difference is, if string = '7f33117cf266a5251', first list comprehension returns ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25', '1'] whereas the second one still returns ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']

Upvotes: 2

Ellisein
Ellisein

Reputation: 1028

1) String "7f33117cf266a525"

string = "7f33117cf266a525"

2) Uppercase "7F33117CF266A525"

string = string.upper()

3) Put it in an array ["7F","33","11","7C","F2","66","A5","25"]

string = [string[i:i+2] for i in range(0, len(string), 2)]

4) Convert it to binary [127,51,17,124,242,102,165,37]

string = [int(x, 16) for x in string]

vice-versa

1) Binary [127,51,17,124,242,102,165,37]

binary = [127,51,17,124,242,102,165,37]

2) Convert it to hex ["7F","33","11","7C","F2","66","A5","25"]

binary = [hex(x)[2:] for x in binary]

3) String "7f33117cf266a525" (it will be already lower-case)

binary = "".join(binary)

Upvotes: 1

Related Questions