日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

base64编解码的类

發(fā)布時(shí)間:2024/4/11 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 base64编解码的类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#pragma once #include <string>class ZBase64 { public:/*編碼DataByte[in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位*/std::string Encode(const unsigned char* Data,int DataByte);/*解碼DataByte[in]輸入的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位OutByte[out]輸出的數(shù)據(jù)長(zhǎng)度,以字節(jié)為單位,請(qǐng)不要通過(guò)返回值計(jì)算輸出數(shù)據(jù)的長(zhǎng)度*/std::string Decode(const char* Data,int DataByte,int& OutByte); }; #include "stdAfx.h" #include "ZBase64.h"using namespace std;string ZBase64::Encode(const unsigned char* Data,int DataByte) {//編碼表const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";//返回值string strEncode;unsigned char Tmp[4]={0};int LineLength=0;for(int i=0;i<(int)(DataByte / 3);i++){Tmp[1] = *Data++;Tmp[2] = *Data++;Tmp[3] = *Data++;strEncode+= EncodeTable[Tmp[1] >> 2];strEncode+= EncodeTable[((Tmp[1] << 4) | (Tmp[2] >> 4)) & 0x3F];strEncode+= EncodeTable[((Tmp[2] << 2) | (Tmp[3] >> 6)) & 0x3F];strEncode+= EncodeTable[Tmp[3] & 0x3F];if(LineLength+=4,LineLength==76) {strEncode+="\r\n";LineLength=0;}}//對(duì)剩余數(shù)據(jù)進(jìn)行編碼int Mod=DataByte % 3;if(Mod==1){Tmp[1] = *Data++;strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4)];strEncode+= "==";}else if(Mod==2){Tmp[1] = *Data++;Tmp[2] = *Data++;strEncode+= EncodeTable[(Tmp[1] & 0xFC) >> 2];strEncode+= EncodeTable[((Tmp[1] & 0x03) << 4) | ((Tmp[2] & 0xF0) >> 4)];strEncode+= EncodeTable[((Tmp[2] & 0x0F) << 2)];strEncode+= "=";}return strEncode; }string ZBase64::Decode(const char* Data,int DataByte,int& OutByte) {//解碼表const char DecodeTable[] ={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,62, // '+'0, 0, 0,63, // '/'52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'0, 0, 0, 0, 0, 0, 0,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, // 'A'-'Z'0, 0, 0, 0, 0, 0,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, // 'a'-'z'};//返回值string strDecode;int nValue;int i= 0;while (i < DataByte){if (*Data != '\r' && *Data!='\n'){nValue = DecodeTable[*Data++] << 18;nValue += DecodeTable[*Data++] << 12;strDecode+=(nValue & 0x00FF0000) >> 16;OutByte++;if (*Data != '='){nValue += DecodeTable[*Data++] << 6;strDecode+=(nValue & 0x0000FF00) >> 8;OutByte++;if (*Data != '='){nValue += DecodeTable[*Data++];strDecode+=nValue & 0x000000FF;OutByte++;}}i += 4;}else// 回車(chē)換行,跳過(guò){Data++;i++;}}return strDecode; } // tt.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 //#include "stdafx.h" #include "ZBase64.h"int _tmain(int argc, _TCHAR* argv[]) {ZBase64 zBase;char sztemp[]="1234567899";std::string en = zBase.Encode((unsigned char *)sztemp,10);char sztemp2[]="MTIzNDU2Nzg5OQ==";int outlen = 0;std::string en2 = zBase.Decode((char *)sztemp2,strlen(sztemp2),outlen);return 0; }


總結(jié)

以上是生活随笔為你收集整理的base64编解码的类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。