1. Mohammad Almasri
  2. PowerBuilder
  3. Tuesday, 30 November 2021 05:05 AM UTC

I would like to convert Hex value to Base64 String, I found one function in PB 2021 to convert but unfortunatlly it is input the Hex value as string so it converting from String to Base64 Not from Hex to Base64.

Any help to do that.

 

Thank you

Accepted Answer
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Tuesday, 30 November 2021 10:45 AM UTC
  2. PowerBuilder
  3. # Permalink

Your initial Hex string is already encoded, so if you want to get the same result as in Javascript, you first have to decode it:

 

//the (correct format) from Javascript Hex encoding to Base64:
//AQpCYXNlbVN0b3JlAg8zMDA2MDA5NjU0MDAwMzMDEzExLzI5LzIwMjEgMTU6MDE6MzYEBTg2LjI1BQUxMS4yNQ==

String ls_Base64Str, ls_HexStr, ls_decoded
blob lblb_data
CoderObject lnv_CoderObject1

lnv_CoderObject1 = Create CoderObject
ls_HexStr = "010A426173656D53746F7265020F333030363030393635343030303333031331312F32392F323032312031353A30313A3336040538362E3235050531312E3235"

// you first have to decode the hex encoded string:
//ls_decoded = string(lnv_CoderObject1.HexDecode(ls_hexstr), encodingutf8!)
//lblb_data = Blob(ls_decoded, encodingutf8!)

// or ... (as pointed out by René, you can do it in one go):
lblb_data = lnv_CoderObject1.HexDecode(ls_hexstr)

// now encode as Base64:
ls_Base64Str = lnv_CoderObject1.Base64Encode(lblb_data)

destroy lnv_CoderObject1

 

regards

Comment
  1. Miguel Leeuwe
  2. Tuesday, 30 November 2021 11:37 AM UTC
I mean what encoding will that blob (as a result of HexDecode()) be?

If I look at the example in PB help it suggests it would be Ansi encoded?



Blob lblb_data

String ls_HexStr



CoderObject lnv_CoderObject

lnv_CoderObject = Create CoderObject



// ls_HexStr = lnv_CoderObject.HexEncode(Blob("Test Hex", EncodingANSI!))

ls_HexStr = "5465737420486578"



lblb_data = lnv_CoderObject.HexDecode(ls_HexStr)

messagebox("HexDecode", string(lblb_data, EncodingANSI!))

  1. Helpful
  1. René Ullrich
  2. Tuesday, 30 November 2021 11:56 AM UTC
It depends on how the Hex was created. Assume you have a string. If you convert it to Hex you may use different encodings. Dependend on encoding the hex value may be different. in the example the encoding was ANSI. But it is also possible to use other encodings.

For HexDecode function it doesn't matter what the Hex value was before. It could also be that it was some binary.

So you can't say what encoding is the result blob. Or you may say: It is the same encoding it was used before. ;-)
  1. Helpful 2
  1. Miguel Leeuwe
  2. Tuesday, 30 November 2021 11:59 AM UTC
Makes sense. Thanks!
  1. Helpful
There are no comments made yet.
Mohammad Almasri Accepted Answer Pending Moderation
  1. Tuesday, 30 November 2021 16:45 PM UTC
  2. PowerBuilder
  3. # 1

you are correct, now it is coming correct as required 
appreciate your precious help

Comment
There are no comments made yet.
Mohammad Almasri Accepted Answer Pending Moderation
  1. Tuesday, 30 November 2021 16:43 PM UTC
  2. PowerBuilder
  3. # 2

Miguel Leeuwe you are correct, now it is coming correct as required 
appreciate your precious help

Comment
There are no comments made yet.
Sivaprakash BKR Accepted Answer Pending Moderation
  1. Tuesday, 30 November 2021 05:57 AM UTC
  2. PowerBuilder
  3. # 3

Hello,

I presume that the Hex value is hex-encoded. In that case

1.  HexDecode -> to convert the hex value to blob
2.  base64encode  -> to convert the resultant blob to base64encode

HTH

 

Comment
  1. Miguel Leeuwe
  2. Tuesday, 30 November 2021 10:46 AM UTC
Sorry Sivaprakash, I didn't realise you already give this correct answer!

regards
  1. Helpful
  1. Sivaprakash BKR
  2. Tuesday, 30 November 2021 11:56 AM UTC
Miguel,

I feel yours is better, with code snippet(s).
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 30 November 2021 18:02 PM UTC
lol, well the important thing is that it works now.
  1. Helpful
There are no comments made yet.
Mohammad Almasri Accepted Answer Pending Moderation
  1. Tuesday, 30 November 2021 05:45 AM UTC
  2. PowerBuilder
  3. # 4

to make it more clear:

input value (Hex): 

010A426173656D53746F7265020F333030363030393635343030303333031331312F32392F323032312031353A30313A3336040538362E3235050531312E3235

 

if I encode it by PB2021 function I will get: (Wrong format)

MDEwYTQyNjE3MzY1NmQ1Mzc0NmY3MjY1MDIwZjMzMzAzMDM2MzAzMDM5MzYzNTM0MzAzMDMwMzMzMzAzMTMzMTMxMmYzMjM5MmYzMjMwMzIzMTIwMzEzNTNhMzAzMTNhMzMzNjA0MDUzODM2MmUzMjM1MDUwNTMxMzEyZTMyMzU=

 

the (correct format) from Javascript Hex encoding to Base64:

AQpCYXNlbVN0b3JlAg8zMDA2MDA5NjU0MDAwMzMDEzExLzI5LzIwMjEgMTU6MDE6MzYEBTg2LjI1BQUxMS4yNQ==

 

the script in PB2021

String ls_Base64Str, ls_IV_Value

lblb_data = Blob(Lower(ls_HexStr), EncodingUTF8!)
CoderObject lnv_CoderObject1
lnv_CoderObject1 = Create CoderObject
ls_Base64Str = lnv_CoderObject1.Base64Encode(lblb_data)

Comment
  1. Praburaj KarthiKeyan
  2. Thursday, 16 December 2021 04:38 AM UTC
Hi Mr Mohammad Almasri,

Any solution for this problem ?

i am also face the same issue. Please post if any solution .
  1. Helpful
  1. Miguel Leeuwe
  2. Thursday, 16 December 2021 06:52 AM UTC
Hi,

Just read this page I would suggest.

regards
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.