BASE64 Decode with Charset GB2312

Question:
I have a Base64 decode error, as follows:

CkString str;
str.setString("16q");
str.base64Decode("gb2312");
const char *strResult = str.getString();

convert result is { cb f2 }
But the correct result should be { d7 aa}

What’s wrong?

The platform is WinCE 6.0, use Chilkat_PPC_M5.lib

Answer:

The following code shows how to do it correctly:

    CkString str;
    str.setString("16q");

    // The following line of code tells the CkString object to 
    // decode the base64 to raw bytes, then interpret those bytes as
    // GB2312 encoded characters and store them within the string.
    // Internally, the string is stored as utf-8.
    str.base64Decode("gb2312");

    // This is an implicit conversion to ANSI, because
    // getString returns either ANSI or utf-8,
    // depending on the setting of get_Utf8/put_Utf8
    const char *strAnsi = str.getString();

    // Instead, fetch the string as GB2312 bytes:
    const char *strGb2312 = str.getEnc("gb2312");

    const unsigned char *c = (const unsigned char *) strGb2312;
    while (*c != '\0') {  printf("%02x ",*c); c++; }
    printf("\n");

    // The output is "d7 aa "


    // Another way to decode using CkByteData...
    CkByteData data;
    data.appendEncoded("16q","base64");
    c = data.getData();
    unsigned long i;
    unsigned long sz = data.getSize();
    for (i=0; i<sz; i++) { printf("%02x ",*c); c++; }
    printf("\n");

    // The output is "d7 aa "

VB6 – SHA-1 Hash and Base64 Encode

Private Sub Command1_Click()

    Dim crypt As New ChilkatCrypt2
    
    success = crypt.UnlockComponent("30-day trial")
    If (success = 0) Then
        MsgBox crypt.LastErrorText
        Exit Sub
    End If
    
    Dim plainText As String
    plainText = "To be SHA-1 hashed..."
    
    crypt.EncodingMode = "base64"
    
    ' Hash the string and base64-encode in 1-Step
    Text1.Text = crypt.HashStringENC(plainText)
    
    ' Now do it in 2-steps. Should produce the same results as for HashStringENC...
    ' Hash the string to get the raw hash bytes, then
    ' base64 encode:
    Dim hashBytes As Variant
    hashBytes = crypt.HashString(plainText)
    
    ' Now Base64 encode:
    Text2.Text = crypt.Encode(hashBytes, "base64")
    
End Sub