pythonrubypackbinascii

Ruby pack('H*') equivalent in Python


I'm having a hard time figuring out why outputs aren't the same. Notice that the difference is very tiny if you compare both OUT's. What I'm trying to achieve is have the same output in Python to the one in Ruby.

Ruby:

IN:['034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192'].pack('H*')

OUT:\x03AQ\xA3\xECF\xB5g\nh+\nc9O\x865\x87\xD1\xBC\x97H;\elp\xEBX\xE7\xF0\xAE\xD1\x92

Python:

IN:unhexlify('034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192') OUT:\x03AQ\xa3\xecF\xb5g\nh+\nc9O\x865\x87\xd1\xbc\x97H;\x1blp\xebX\xe7\xf0\xae\xd1\x92


Solution

  • Both languages have produced the same sequence of bytes; the only difference is in the way they display those bytes to you. Ruby has an \e string escape sequence for the 1b byte (ASCII ESC, escape), while Python displays that byte as \x1b.