1

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?

1
  • 1
    [string[i:i+2] for i in range(0, len(string), 2)] Commented Mar 5, 2019 at 6:56

5 Answers 5

2

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'
Sign up to request clarification or add additional context in comments.

2 Comments

Should mention python3 only, just incase :)
Indeed. Updated my answer with the note then.
2

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']

Comments

2

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']

Comments

1

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.