Well, you'd better have the full code as the time limit is about to expire and the application will start tomorrow.
You can change the fields that meet your requirements to replace the declared values:
static void UTF8ToHexThenBase64(Args _args)
{
str NameAlias = 'شركة أ ب ج المحدودة', //Hamad M. ALDrees & Partners for Industry & Mining', //
CoRegNum = '300012345600003',
DT = '31/10/2021T01:05:10 pm',
Total = '268927.55',
VatAmnt = '35077.51',
V1Hex,V2Hex,V3Hex,V4Hex,V5Hex,
L1Hex,L2Hex,L3Hex,L4Hex,L5Hex,
UTF8ComName;
int L1,L2,L3,L4,L5,LCHex,a,j;
Str1260 VHexString,VBase64String;
////To convert UTF8 strings into bytes
System.Text.Encoding encoding = System.Text.Encoding::get_UTF8();
System.Byte byte;
System.Byte[] bytes1, bytes2, bytes3, bytes4, bytes5, bytesBase64;
//// Get QR string
///Getting the LV of the first field
//Convert the first UTF8 into byte array
bytes1 = encoding.GetBytes(NameAlias);
//Get the length of first UTF8 string in Hex
L1Hex = int2Hex(bytes1.get_Length(),2);
//To convert bytes1 into hex
V1Hex = System.BitConverter::ToString(bytes1);
//To elemenate the sign '-' (If any) from the resulted hex string
V1Hex = strReplace(V1Hex, "-", "");
//Repeating the same with remaining fields
bytes2 = encoding.GetBytes(CoRegNum);
L2Hex = int2Hex(bytes2.get_Length(),2);
V2Hex = System.BitConverter::ToString(bytes2);
V2Hex = strReplace(V2Hex, "-", "");
bytes3 = encoding.GetBytes(DT);
L3Hex = int2Hex(strlen(DT),2);
V3Hex = System.BitConverter::ToString(bytes3);
V3Hex = strReplace(V3Hex, "-", "");
bytes4 = encoding.GetBytes(Total);
L4Hex = int2Hex(strlen(Total),2);
V4Hex = System.BitConverter::ToString(bytes4);
V4Hex = strReplace(V4Hex, "-", "");
bytes5 = encoding.GetBytes(VatAmnt);
L5Hex = int2Hex(strlen(VatAmnt),2);
V5Hex = System.BitConverter::ToString(bytes5);
V5Hex = strReplace(V5Hex, "-", "");
//GET the whole TLV string
VHexString = '01' L1Hex V1Hex '02' L2Hex V2Hex '03' L3Hex V3Hex '04' L4Hex V4Hex '05' L5Hex V5Hex;
//To convert the TLV string into Base64
LCHex = strLen(VHexString) / 2;
bytesBase64 = encoding.GetBytes(subStr(VHexString, 1, LCHex));
bytesBase64.Clear();
for (a = 1; a <= strLen(VHexString); a )
{
byte = System.Convert::ToByte(hex2Int(subStr(VHexString, a, 2)));
bytesBase64.SetValue(byte, j);
a ;
j ;
}
VBase64String = System.Convert::ToBase64String(bytesBase64);
info(strFmt('TLV String: %1',VHexString));
info(strFmt('Base64 string: %1',VBase64String));
}
I tried to shorten the code as much as possible to make it easier.
I hope that code will help everyone. If anybody has an idea to enhance it more, please, share it with us.
Yours,
Ahmed Siliem