I tried to recreate the scenario by first converting a text, decimal and date fields into hexadecimal
and further converting the concatenated result value to Base64 by using Base64Convert Codeunit.
But the BC Base64 conversion is giving different result than the actual Hex to Base64 conversion result from online.
Later I realized the the Hex results which I am storing in text variable in BC is actually converting text to Base64 instead of Hex to Base64.
Could you please guide me how to store a Hexadecimal in BC and convert it to Base64 ?
Thanks In Advance.
First I must say that I never tried something like this.
I think the problem is that in BC we don’t have hexadecimal variables, and what you converted to Base64, probably is a text representation of a hexadecimal values, and this is the problem the Base64 generated from BC is not matching the Hex to Base64 conversion from online tool.
Hexadecimal is a representation of a number (a number in a 16th base)… you can try saving your data in a char variables (as a numbers from 0 to 15) and then try to convert this chars values in a Base64.
If this doesn’t work, you can create an Azure function to do this (or some API function in some system where you can do this), and use this Azure (or API) function to do this conversion.
Thinking more about it, try using Byte variable, and 2 integers to represent 2 hex values and then save to a OutStream:
Int1 := [number from 0 to 15] * 16;
Int2 := [number from 0 to 15];
MyByte := Int1 + Int2;
Once you have write all your hex values in MyOutStream var, you can convert the Stream into Base64.
To store a hexadecimal value in Business Central (BC) and convert it to Base64, you can use the Binary data type instead of Text data type. Here are the general steps you can follow:
- Convert the text, decimal, and date fields to hexadecimal format using the appropriate conversion functions in BC, such as FORMAT or FORMATHEX.
- Concatenate the hexadecimal values into a single string, if necessary.
- Convert the concatenated hexadecimal string to a binary value using the HEX2BIN function.
- Convert the binary value to Base64 using the Base64Convert Codeunit.
Here is an example code snippet that demonstrates these steps:
// Convert text, decimal, and date fields to hexadecimal
Text := FORMAT(Text);
Decimal := FORMATHEX(Decimal,0);
Date := FORMAT(Date,0,‘’);
// Concatenate hexadecimal values into a single string
HexValue := Text + Decimal + Date;
// Convert concatenated hexadecimal string to binary value
BinaryValue := HEX2BIN(HexValue);
// Convert binary value to Base64
Base64Value := Base64Convert.ToBase64(BinaryValue);
Note that the HEX2BIN function requires a valid hexadecimal string as input. Make sure that you have converted the fields to hexadecimal correctly and that the concatenated string is a valid hexadecimal string. Additionally, check the settings of the Base64Convert Codeunit to ensure that it is using the correct encoding and padding options for your needs.