-1

I have a list of numbers:

a = [2, 2, 30, 1, 30, 6, 3, 30, 0, 9, 4, 30, 1, 30, 1, 29]

I am trying to convert the list from integers to ascii characters of the same number before converting them to hex. I am running into trouble figuring out how to convert the entire list when there are some numbers with more than one digit.

I have tried:

a = [2, 2, 30, 1, 30, 6, 3, 30, 0, 9, 4, 30, 1, 30, 1, 29]
b = f'0x{ord(str(a)):x}'  
c = int(b, base=16)

which throws the error: ord() expected a character, but string of length 2 found.

A variation of it:

a = [2, 2, 30, 1, 30, 6, 3, 30, 0, 9, 4, 30, 1, 30, 1, 29]
separated_digits = [int(digit) for number in a for digit in str(number)]
ascii_chars_hexed = [f'0x{ord(str(digit)):x}' for digit in separated_digits]

works if I split the numbers in a into single digits, but I need the character output of the double digit numbers to be based upon the 2 digits rather than each individually.

I am expecting it to output:

[0x32, 0x32, 0x1e, 0x31, 0x1e, 0x36, 0x33, 0x1e, 0x30, 0x39, 0x34, 0x1e, 0x31, 0x1e, 0x31, 0x1d]`

Any thoughts?

  • 3
    *Why* do you expect that output? What is the reason that 2 becomes 0x32 (ASCII '2') but 30 becomes 0x1e (the hexadecimal value of decimal 30)? – Mark Tolonen Jan 06 '23 at 19:55
  • 1
    What does it mean to "convert from integers to ascii characters of the same number"? The integer 2 in ASCII is not the character `2`, it's an STX char, but I don't think that's what you actually want. And converting a character to a number (hexadecimal or otherwise) would imply that you're just undoing whatever transformation you just did to turn it from a number to a character. Are you just asking how to convert the ints to hexadecimal strings? That's just `[hex(i) for i in a]`, but that's also not your desired output, so it's not clear what exactly you're trying to get. – Samwise Jan 06 '23 at 20:01
  • @MarkTolonen int 2 is meant to become ascii chr 2 which would then turn to its hex: 0x32. The 30 is meant to become ascii chr rs which would then turn into its hex: 0x1e – Sean Mckay Jan 06 '23 at 20:06
  • separate the digits before you str them then – Paul Collingwood Jan 06 '23 at 20:10
  • @Samwise It means int 2 should become ascii char 2, which would then be able to be turned into its hex 0x32. int 30 should become ascii char 30, which would then be able to be turned into its hex 0x1e – Sean Mckay Jan 06 '23 at 20:12
  • @PaulCollingwood I need the output to be based upon the 2 digit ints rather than their individual digits. – Sean Mckay Jan 06 '23 at 20:17
  • @SeanMckay There is no such thing as "ASCII char 30". That's two characters ("3" and "0"). – Samwise Jan 06 '23 at 22:21

1 Answers1

0

It's an odd data format, but the following does what you want. Convert the single-digit numbers to a string and get their ASCII value. It would make more sense to store the ASCII value of the '2' in the string as the integer 50 to begin with, for example.

a = [2, 2, 30, 1, 30, 6, 3, 30, 0, 9, 4, 30, 1, 30, 1, 29]
print([f'{ord(str(n)) if n < 10 else n:#x}' for n in a])

Output:

['0x32', '0x32', '0x1e', '0x31', '0x1e', '0x36', '0x33', '0x1e', '0x30', '0x39', '0x34', '0x1e', '0x31', '0x1e', '0x31', '0x1d']
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • I've played around with having it as a 50 instead of the 2, however I needed to convert variable numbers in the same way so I figured I would do it all at once. Thanks again! – Sean Mckay Jan 06 '23 at 20:21