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 "
admin
0
Tags :