1

I am writing a program for padding oracle attacks and need bytearrays, but if I define a new bytearray the first byte 0x63 gets printed different.

I have to XOR 2 bytearrays bytewise.

test = bytearray( [99,203,00] )
print(test)
print(hex(99))

Output:

bytearray(b'c\xcb\x00')
0x63

This is my first question here. Thanks for your help!

f

leftjoin
  • 36,950
  • 8
  • 57
  • 116
R. K.
  • 13
  • 3
  • Welcome to StackOverflow! Please add you code to the question, so that people who want to help you can copy-paste it to their IDE. Pictures are not so helpful – Sasha Tsukanov Nov 04 '18 at 14:42
  • might be a dupe of https://stackoverflow.com/questions/17093700/python-bytearray-printing - but its not a 100% – Patrick Artner Nov 04 '18 at 14:57

1 Answers1

0

For string output python replaces printable hexcodes by the chr(hexcode) character for display purposes:

print('c', ord('c'),hex(ord('c')))   #  c 99 '0x63'

t = bytearray([99,203,0])
print(t)                             #  bytearray(b'c\xcb\x00')
print(t[0],t[1],t[2])                #  99 203 0

They are equivalent - but shorter to print out. You can get a all-hex representation like so:

t = bytearray([99,203,0])
t_hex = '\\'+'\\'.join( ( hex(i) for i in t) ) 

print(t_hex)

Output:

\0x63\0xcb\0x0 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69