使用C解码base64/UTF-16LE [英] Decode base64/UTF-16LE using C
问题描述
我的问题是尝试解码base64/UTF-16LE字符串.解码的结果始终是第一个字符.如果将字符串解码为utf-8/ascii/windows1252,则结果正确.
My problem is trying to decode a base64 / UTF-16LE string. The result of this decoding is always the first character. If you decode the string to utf-8 / ascii / windows1252, then the result is correct.
任何人都有现成的解决方案吗?
Can anyone have a ready-made solution?
我使用的来自互联网的标准代码:
The standard code from the internet that I use:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
b64_decode_string("SABlAGwAbABvAA==");
return 0;
}
char base64decode_lut[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
void base64decode(char *src, char *dest, int len)
{
int i=0, slen=strlen(src);
for(i=0;i<slen&&i<len;i+=4,src+=4)
{
char c1=base64decode_lut[*src], c2=base64decode_lut[*(src+1)], c3=base64decode_lut[*(src+2)], c4=base64decode_lut[*(src+3)];
*(dest++)=(c1&0x3F)<<0x2|(c2&0x30)>>0x4;
*(dest++)=(c3!=64)?((c2&0xF)<<0x4|(c3&0x3C)>>0x2):'\0';
*(dest++)=(c4!=64)?((c3&0x3)<<0x6|c4&0x3F):'\0';
}
*dest='\0'; // Append terminator
}
int b64_decode_string(char *source)
{
int dest_size;
int res;
char *dest;
dest_size = strlen(source);
dest = (char *)malloc(dest_size);
memset(dest,0,dest_size);
base64decode(source, dest, dest_size);
printf("Decode: %s", dest);
}
推荐答案
仅第一个字符被解码的原因是因为第二个字符以0x00字节开头,所以对strlen()
的调用(仅用于ascii字符串) )返回的数字非常小(例如1或2),而不是字符串的实际长度.
The reason only the first character was decoded is because the second character starts with a 0x00 byte, so the call to strlen()
(which is only for ascii strings) returned a very small number (like 1 or 2) rather than the actual length of the string.
建议您熟悉wchar_t
类型和处理宽字符的函数,例如:wprintf()
和字符串的'wide'修饰符:-L
和头文件:wchar.h
suggest becoming familiar with the wchar_t
type and the functions to handle wide characters, like: wprintf()
and the 'wide' modifier for strings: -L
and the header file: wchar.h
这篇关于使用C解码base64/UTF-16LE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!