1

I'm using a weird blockchain and it's API returns data like this:

0x36333562663261376537333436376636333363313931353738613938383137313663383633306235373164613034643634303334356331646232333231636537

Now, i know this is a string I sent in that was actually 64 hex as a string And I get back 128 chars (plus 0x) as the above.

So can anyone suggest how to decode this? since its all numbers, i'm assuming it's something base10, but then we would have different byte lengths (64 vs 128) etc.

Since there are lots of 2s and 3s i was guessing maybe that is an indicator for a byte of 2 vs 3

Is it a unicode array of some type? The '0x' at the front makes me think it's hex values but there's no actual DEADBEEF here...

now I know the original string i passed in was 64 hex characters. I'm getting back 128 decimals.

I'm looking for a converter in JS to get back the hex 'string'

I've tried atob and Buffer but haven't cracked it yet...

Javascript - Converting between Unicode string and ArrayBuffer

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
dcsan
  • 11,333
  • 15
  • 77
  • 118

1 Answers1

2

If you look at the hex pairs (representing each byte), the hex string you sent in originally has been re-hexified or re-encoded as hex. The first 5 bytes of the returned string, for example, look like this:

3633356266 -> 36 33 35 62 66

And these are all valid ASCII characters, decoding to:

0x36 -> '6'
0x33 -> '3'
0x35 -> '5'
0x62 -> 'b'
0x66 -> 'f'

So you can see, your original hex string (starting with 635bf) was re-encoded into hexadecimal again and sent back to you - essentially, it was treated as a string of characters and not a string of hex values (which I believe is what you intended them to be interpreted as).

To decode it, you can do this very simply by splitting it into 2-character blocks and getting the ASCII code point with that value, then re-joining it all together (also trim the '0x' off of the start):

const returnedStr = "0x36333562663261376537333436376636333363313931353738613938383137313663383633306235373164613034643634303334356331646232333231636537";

let hexBytes = returnedStr.slice(2).match(/.{1,2}/g);

let decodedStr = hexBytes.reduce((str, curr) => str + String.fromCharCode(parseInt(curr, 16)), "");

console.log(decodedStr);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    holy kangaroos jack! you're right! in retrospect its obvious the 3s and 2s are cos it was low single digit hexes all re-encoded, the old double hex sandwich! thanks and I owe you a beer post-covid!!! – dcsan Dec 14 '21 at 07:16
  • i was confused cos it didn't look like hex, but it is re-hexed hex. what kind of witchcraft is this :D – dcsan Dec 14 '21 at 07:18
  • Glad I could help. Very strange API service I would recommend finding some actual documentation for it :) – Jack Bashford Dec 14 '21 at 11:01
  • Turns out the data going into it was already hex encoded, so I needed to tell it to NOT encode again... – dcsan Dec 16 '21 at 19:26