Make a byte64 String from an UTF-8 String in AX2012
A small “how to” about the conversion between a byte64 String and an UTF-8 String in AX2012 and (because very similar) in C#.
byte64 String to UTF-8
AX2012:
static void strToBase64Str(Args _args) | |
{ | |
System.Byte[] byte; | |
str strUTF8Input = "AX2012R3CU9", | |
str64; | |
System.Text.Encoding encoding = System.Text.Encoding::get_UTF8(); | |
// UTF8 input | |
byte = encoding.GetBytes(strUTF8Input); | |
// base64 Output | |
str64 = System.Convert::ToBase64String(byte); | |
// result | |
info(strFmt("UTF8 input: %1", strUTF8Input)); | |
info(strFmt("base64: %1", str64)); | |
} |
C#:
public class Example | |
{ | |
public void main() | |
{ | |
byte[] bytes = Encoding.UTF8.GetBytes("AX2012R3CU9"); | |
string base64 = Convert.ToBase64String(bytes); | |
Console.WriteLine(base64); | |
} | |
} | |
// Output: | |
// QVgyMDEyUjNDVTk= |

UTF-8 String to byte64
Please note: This will only work if you know that the bytes are encoded as UTF-8.
AX2012:
static void strToBase64Str(Args _args) | |
{ | |
System.Byte[] byte; | |
str str64 = "QVgyMDEyUjNDVTk=", | |
strUTF8Output; | |
System.Text.Encoding encoding = System.Text.Encoding::get_UTF8(); | |
// base64 Input | |
byte = System.Convert::FromBase64String(str64); | |
// UTF8 output | |
strUTF8Output = encoding.GetString(byte); | |
// result | |
info(strFmt("base64: %1", str64)); | |
info(strFmt("UTF8 output: %1", strUTF8Output)); | |
} |
C#: