1

I have a line of code in java that am using to get ascii representation of a string and then extract the bytes to a buffer. but, after processing the bytes, the resulting bytes array kind of double in size.

byte[] id = {1, 64, 71, -17, -65, -67};
ByteBuf response = Unpooled.buffer();
response.writeBytes(ByteBufUtil.hexDump(id).getBytes(StandardCharsets.US_ASCII));
response.readableBytes(); //12

response: {48, 49, 52, 48, 52, 55, 101, 102, 98, 102, 98, 100}

so am confused as to how 6bytes became 12 bytes... not that i really care about the size, but am trying to do the same in python, and annoyingly the message length gets inconsistent..

  • thats interesting... funny, one can arrive at the same conclusion in python in the same length of data. do you have any idea of the concept behind the conversion, prolly i can replicate that. – Adebari Olalekan Sep 08 '21 at 19:01
  • You seem to be using non-standard classes. Please edit your code or your tags as appropriate. – WJS Sep 08 '21 at 19:46
  • 1
    yea am using netty library in java, which is where `ByteBuf` and `Unpooled` comes from – Adebari Olalekan Sep 08 '21 at 19:49

1 Answers1

0

From the question I understand you want to convert some bytes to hex and back to the original bytes. Your code is wrong there. As others pointed out you convert from bytes to hex, but you do not use any reverse operation to go from the hex string back to the original bytes. In your code you simply use the hex string's byte representation and go further, doubling the length.

What you need to do instead is mentioned at Convert a string representation of a hex dump to a byte array using Java?

Queeg
  • 7,748
  • 1
  • 16
  • 42
  • Thanks, i think that makes sense. the code is a legacy code that am trying to port to python, and byte length just wouldn't be the same. – Adebari Olalekan Sep 13 '21 at 23:56