windows有可直接调用的base64编码解码函数吗

merry_wanwan 2011-07-15 01:24:38
windows有可直接调用的base64编码解码函数吗?

要对wchar_t的字符串进行编码和解码
...全文
561 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-12-10
  • 打赏
  • 举报
回复
学习4楼!
赵4老师 2013-12-10
  • 打赏
  • 举报
回复
strlen, wcslen, _mbslen, _mbstrlen Get the length of a string. size_t strlen( const char *string ); size_t wcslen( const wchar_t *string ); size_t _mbslen( const unsigned char *string ); size_t _mbstrlen( const char *string ); Routine Required Header Compatibility strlen <string.h> ANSI, Win 95, Win NT wcslen <string.h> or <wchar.h> ANSI, Win 95, Win NT _mbslen <mbstring.h> Win 95, Win NT _mbstrlen <stdlib.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns the number of characters in string, excluding the terminal NULL. No return value is reserved to indicate an error. Parameter string Null-terminated string Remarks Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tcslen strlen strlen wcslen _tcsclen strlen _mbslen wcslen _mbslen and _mbstrlen return the number of multibyte characters in a multibyte-character string. _mbslen recognizes multibyte-character sequences according to the multibyte code page currently in use; it does not test for multibyte-character validity. _mbstrlen tests for multibyte-character validity and recognizes multibyte-character sequences according to the LC_CTYPE category setting of the current locale. For more information about the LC_CTYPE category, see setlocale. Example /* STRLEN.C */ #include <string.h> #include <stdio.h> #include <conio.h> #include <dos.h> void main( void ) { char buffer[61] = "How long am I?"; int len; len = strlen( buffer ); printf( "'%s' is %d characters long\n", buffer, len ); } Output 'How long am I?' is 14 characters long String Manipulation Routines | Locale Routines See Also setlocale, strcat, strcmp, strcoll Functions, strcpy, strrchr, _strset, strspn
雪月峰花 2013-12-10
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong1 的回复:
base64编/解码的功能是将取值范围在0x00..0xFF内的字节流转换/反转为取值范围在'A..Za..z0..9+/='中的字符。 和“取值范围在0x00..0xFF内的字节流”具体表示Unicode字符串、UTF8字符串、ASCII字符串;JPG图片、MPEG视频、BIOS映像、硬盘Ghost、……没有关系!
请问赵老师,2楼给的程序里,bdlen = strlen(bdata);显然Unicode不适用,是不是吧strlen换成 wcslen就可以了
merry_wanwan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 luciferisnotsatan 的回复:]
unicode是宽字符,强转char*不就行了
[/Quote]
感谢!搞明白了
merry_wanwan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zhao4zhong1 的回复:]
base64编/解码的功能是将取值范围在0x00..0xFF内的字节流转换/反转为取值范围在'A..Za..z0..9+/='中的字符。
和“取值范围在0x00..0xFF内的字节流”具体表示Unicode字符串、UTF8字符串、ASCII字符串;JPG图片、MPEG视频、BIOS映像、硬盘Ghost、……没有关系!
[/Quote]
明白了,感谢
赵4老师 2011-07-15
  • 打赏
  • 举报
回复
base64编/解码的功能是将取值范围在0x00..0xFF内的字节流转换/反转为取值范围在'A..Za..z0..9+/='中的字符。
和“取值范围在0x00..0xFF内的字节流”具体表示Unicode字符串、UTF8字符串、ASCII字符串;JPG图片、MPEG视频、BIOS映像、硬盘Ghost、……没有关系!
merry_wanwan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 zhao4zhong1 的回复:]
TCHAR才会随UNICODE设置在char和wchar之间切换吧。
[/Quote]
是的,我只是想问
char* base64_encode(const char *data) 

为什么他们不用unicode的

wchar_t* base64_encode(const wchar_t *data) 
赵4老师 2011-07-15
  • 打赏
  • 举报
回复
TCHAR才会随UNICODE设置在char和wchar之间切换吧。
merry_wanwan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 luciferisnotsatan 的回复:]
引用 5 楼 merry_wanwan 的回复:

引用 2 楼 q191201771 的回复:
http://blog.csdn.net/q191201771/article/details/6103582
我在用的

这个我原来也在用
2个都能用

我有点糊涂了
楼上两位不用unicode编码吗?
unicode编码的字符串进行base64之后和以上得到的结果是不同的
……
[/Quote]
有点搞不懂
char* aa = "abcd"; //如果工程设置为unicode,这算是unicode字符串吗
wchar_t* bb = L"abcd"; //这样呢?

luciferisnotsatan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 merry_wanwan 的回复:]

引用 2 楼 q191201771 的回复:
http://blog.csdn.net/q191201771/article/details/6103582
我在用的

这个我原来也在用
2个都能用

我有点糊涂了
楼上两位不用unicode编码吗?
unicode编码的字符串进行base64之后和以上得到的结果是不同的
[/Quote]
unicode是宽字符,强转char*不就行了
merry_wanwan 2011-07-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q191201771 的回复:]
http://blog.csdn.net/q191201771/article/details/6103582
我在用的
[/Quote]
这个我原来也在用
2个都能用

我有点糊涂了
楼上两位不用unicode编码吗?
unicode编码的字符串进行base64之后和以上得到的结果是不同的
healer_kx 2011-07-15
  • 打赏
  • 举报
回复
关键是第三个参数:

编码,解码可以参照下面两个函数搞:
static CStringA Encode (const void* pBuffer, DWORD nSize)
{
DWORD nNeed = 0 ;
CryptBinaryToStringA ((const BYTE*)pBuffer, nSize, CRYPT_STRING_BASE64, NULL, &nNeed) ;

CStringA s ;
if (nNeed)
{
std::vector<char> buf (nNeed) ;
CryptBinaryToStringA ((const BYTE*)pBuffer, nSize, CRYPT_STRING_BASE64, &buf[0], &nNeed) ;
s = &buf[0] ;
}
s.Replace ("\r\n", "") ; // default will add CR/LF
return s ;
}

static void Decode (const char* sBase64, std::vector<BYTE>& decoded_data)
{
decoded_data.clear() ;

if (sBase64)
{
DWORD nNeed = 0 ;
CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, NULL, &nNeed, NULL, NULL) ;
if (nNeed)
{
decoded_data.resize (nNeed) ;
CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, &decoded_data[0], &nNeed, NULL, NULL) ;
}
}
}
healer_kx 2011-07-15
  • 打赏
  • 举报
回复
咕~~(╯﹏╰)b
Windows确实 提供了这个API。


CryptBinaryToStringA (buffer, size, CRYPT_STRING_BASE64, NULL, &need) ;


就想叫yoko 2011-07-15
  • 打赏
  • 举报
回复
http://blog.csdn.net/q191201771/article/details/6103582
我在用的
至善者善之敌 2011-07-15
  • 打赏
  • 举报
回复

/* *****************************************************************************

* Description : BASE64 encoding and decoding

* Date : 06-08-21 21:00

* Author : aishen944

* Copryright :

********************************************************************************/

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#define BASE64_PAD64 '='

char base64_alphabet[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
'2', '3', '4', '5', '6', '7', '8', '9', '+',
'/'};

char base64_suffix_map[256] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};

static char cmove_bits(unsigned char src, unsigned lnum, unsigned rnum) {
src <<= lnum;
src >>= rnum;
return src;
}

char* base64_encode(const char *data) {
char *ret, *retpos;
int n, m, padnum = 0, retsize, dlen = strlen(data);

if(dlen == 0) return NULL;

/* Account the result buffer size and alloc the memory for it. */
if((dlen % 3) != 0)
padnum = 3 - dlen % 3;
retsize = (dlen + padnum) + ((dlen + padnum) * 1/3) + 1;
if((ret = malloc(retsize)) == NULL)
return NULL;
retpos = ret;

/* Starting to convert the originality characters to BASE64 chracaters.
Converting process keep to 4->6 principle. */
for(m = 0; m < (dlen + padnum); m += 3) {
/* When data is not suffice 24 bits then pad 0 and the empty place pad '='. */
*(retpos) = base64_alphabet[cmove_bits(*data, 0, 2)];
if(m == dlen + padnum - 3 && padnum != 0) { /* Whether the last bits-group suffice 24 bits. */
if(padnum == 1) { /* 16bit need pad one '='. */
*(retpos + 1) = base64_alphabet[cmove_bits(*data, 6, 2) + cmove_bits(*(data + 1), 0, 4)];
*(retpos + 2) = base64_alphabet[cmove_bits(*(data + 1), 4, 2)];
*(retpos + 3) = BASE64_PAD64;
} else if(padnum == 2) { /* 8bit need pad two'='. */
*(retpos + 1) = base64_alphabet[cmove_bits(*data, 6, 2)];
*(retpos + 2) = BASE64_PAD64;
*(retpos + 3) = BASE64_PAD64;
}
} else { /* 24bit normal. */
*(retpos + 1) = base64_alphabet[cmove_bits(*data, 6, 2) + cmove_bits(*(data + 1), 0, 4)];
*(retpos + 2) = base64_alphabet[cmove_bits(*(data + 1), 4, 2) + cmove_bits(*(data + 2), 0, 6)];
*(retpos + 3) = base64_alphabet[*(data + 2) & 0x3f];
}

retpos += 4;
data += 3;
}

ret[retsize - 1] =0;

return ret;
}

char* base64_decode(const char *bdata) {
char *ret = NULL, *retpos;
int n, m, padnum = 0, retsize, bdlen = strlen(bdata);

if(bdlen == 0) return NULL;
if(bdlen % 4 != 0) return NULL;

/* Whether the data have invalid base-64 characters? */
for(m = 0; m < bdlen; ++m) {
if(bdata[m] != BASE64_PAD64 && base64_suffix_map[bdata[m]] == 255)
goto LEND;
}

/* Account the output size. */
if(bdata[bdlen - 1] == '=') padnum = 1;
if(bdata[bdlen - 1] == '=' && bdata[bdlen - 2] == '=') padnum = 2;
retsize = (bdlen - 4) - (bdlen - 4) / 4 + (3 - padnum) + 1;
ret = malloc(retsize);
if(ret == NULL)
return NULL;
retpos = ret;

/* Begging to decode. */
for(m = 0; m < bdlen; m += 4) {
*retpos = cmove_bits(base64_suffix_map[*bdata], 2, 0) + cmove_bits(base64_suffix_map[*(bdata + 1)], 0, 4);
if(m == bdlen - 4 && padnum != 0) { /* Only deal with last four bits. */
if(padnum == 1) /* Have one pad characters, only two availability characters. */
*(retpos + 1) = cmove_bits(base64_suffix_map[*(bdata + 1)], 4, 0) + cmove_bits(base64_suffix_map[*(bdata + 2)], 0, 2);
/*
Have two pad characters, only two availability characters.
if(padnum == 2) {
}
*/
retpos += 3 - padnum;
} else {
*(retpos + 1) = cmove_bits(base64_suffix_map[*(bdata + 1)], 4, 0) + cmove_bits(base64_suffix_map[*(bdata + 2)], 0, 2);
*(retpos + 2) = cmove_bits(base64_suffix_map[*(bdata + 2)], 6, 0) + base64_suffix_map[*(bdata + 3)];
retpos += 3;
}
bdata += 4;
}

ret[retsize - 1] = 0;

LEND: return ret;
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧