3

I am currently working on a custom SAPUI5 app. I would like to make a service call, which expects the Guid in a different format than it is currently available.

An example:

  • Available (base64): QvLAUUzUCZbhAAAAjSS2iA==
  • As it should be (hexadecimal): 42F2C0514CD40996E10000008D24B688

I have not found an online decoder, which could produce the desired result, however, I was able to encode the guid 42F2C0514CD40996E10000008D24B to QvLAUUzUCZbhAAAAjss2iA== with the SAP ABAP standard function module HTTP_BASE64_ENCODE. With the usual online encoders, however, I got a different result.

How can I decode the encoded guid with JavaScript so that it has the desired format?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
C. Ubkcah
  • 273
  • 13
  • 33
  • 1
    `atob("QvLAUUzUCZbhAAAAjSS2iA==").replace(/[^]/g, c => c.charCodeAt(0).toString(16))` – Thomas May 29 '19 at 12:42
  • Hi Thomas. Thank you for the answer, this looks already quite good! There is only something wrong with zeros. Your function returns "42f2c0514cd4996e10008d24b688" and it should be "42F2C0514CD4**0**996E1000**000**8D24B688". I marked the missing ones bold. It seems like they get lost... – C. Ubkcah May 29 '19 at 13:21
  • And could you please tell me something about the meaning of the regex and charCodeAt? Thank you! – C. Ubkcah May 29 '19 at 13:39
  • 1
    The regex is replacing all characters for its hexadecimal representation. – filipe May 29 '19 at 14:01
  • For your information, this online decoder/encoder gives the right result: https://paulschou.com/tools/xlate/ – Sandra Rossi May 29 '19 at 17:08
  • 1
    Possible duplicate of [Decode Base64 to Hexadecimal string with javascript](https://stackoverflow.com/questions/39460182/decode-base64-to-hexadecimal-string-with-javascript) – Jordan Running May 30 '19 at 17:58

1 Answers1

1

The string is in hexadecimal format you will have to convert it.

First you convert the string to binary (atob > charCodeAt) and then using the toString(16) you get the hex.

I will not post the code, since its already explained Decode Base64 to Hexadecimal string with javascript

filipe
  • 1,957
  • 1
  • 10
  • 23