Home > OS >  what kind of a buffer is this?
what kind of a buffer is this?

Time:12-14

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

CodePudding user response:

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);

  • Related