分享
 
 
 

uuid从32个文本(0-e)字符转化为16个二进制字符,以及BASE64编码的互相转化 c++源代码

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

// a64.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <string.h>

#include <ctype.h>

#include <stdlib.h>

#include <stdio.h>

#include <windows.h>

static void to64frombits(unsigned char *out, const unsigned char *in, int inlen);

void u16to32(char *c32,unsigned char *c16);

void u32to16(unsigned char *c16,char *c32);

BYTE LMoveBit(int base, int MoveNum) ;

BYTE* Base64Decode(char *base64code, DWORD base64length) ;

int main(int argc, char* argv[])

{

//printf("Hello World!\n");

unsigned char *out=new unsigned char[100];

strcpy((char*)out,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

const unsigned char *in=new unsigned char[100];

strcpy((char*)in,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");

const unsigned char *szuuid=new unsigned char[100];

strcpy((char*)szuuid,"00de8922a3cb4fee95fb41bd2ac8f958");

to64frombits(out,szuuid,43);

printf("UUID32从文本到BASE64编码%s->%s\n",szuuid,out);

strcpy((char*)in,"AbcdEf12123412341234123456789abC");

strcpy((char*)out,"FFFFFFFFFFFFFFFF");

u32to16((unsigned char*)out,(char*)in);

printf("UUID32从文本到二进制%s->%s\n",in,out);

strcpy((char*)in,(char*)out);

strcpy((char*)out,"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");

u16to32((char*)out,(unsigned char*)in);

printf("UUID32从二进制到文本%s->%s\n",in,out);

//strcpy((char*)in,(char*)out);

strcpy((char*)out,"FFFFFFFFFFFFFFFFFFFFFFFF");

to64frombits(out,in,16);

printf("从二进制到BASE64编码%s->%s\n",in,out);

strcpy((char*)in,(char*)out);

strcpy((char*)out,"FFFFFFFFFFFFFFFF");

char * sztemp=(char*)Base64Decode((char *)in, 24);//这里我不太明白,16个字符*4/3=22就够了,但是不输入24就解码不对

strcpy((char*)out,sztemp) ;

delete [] sztemp;

printf("从BASE64编码到二进制%s->%s\n",in,out);

strcpy((char*)in,(char*)out);

strcpy((char*)out,"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");

u16to32((char*)out,(unsigned char*)in);

printf("UUID32从二进制到文本%s->%s\n",in,out);

// delete []szuuid;

// delete []in;

delete []out;

return 0;

}

void u32to16(unsigned char *c16,char *c32)

{

//char bd[]="AbcdEf12123412341234123456789abC";

//unsigned char dd[]=" ";

unsigned char *dd=c16;

char *bd=c32;

for(int i=0;i<32;i+=2)

{

unsigned char c1=bd[i]-(isdigit(bd[i])?'0':55);

unsigned char c2=bd[i+1]-(isdigit(bd[i+1])?'0':55);

dd[i/2]=(c1<<4)+(c2&0xf);

}

dd[16]=0;

}

void u16to32(char *c32,unsigned char *c16)

{

//char ed[]="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF";

char *ed=c32;

unsigned char *dd=c16;

for(int i=0;i<16;i++)

{

unsigned char c3=(dd[i]>>4);

ed[i*2]=c3+(c3<10?'0':55);

c3=(dd[i]&0xf);

ed[i*2+1]=c3+(c3<10?'0':55);

}

ed[32]=0;

}

//base64编码 遥志软件

static void to64frombits(unsigned char *out, const unsigned char *in, int inlen)

{

const char base64digits[] =

"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

for (; inlen >= 3; inlen -= 3)

{

*out++ = base64digits[in[0] >> 2];

*out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];

*out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];

*out++ = base64digits[in[2] & 0x3f];

in += 3;

}

if (inlen > 0)

{

unsigned char fragment;

*out++ = base64digits[in[0] >> 2];

fragment = (in[0] << 4) & 0x30;

if (inlen > 1)

fragment |= in[1] >> 4;

*out++ = base64digits[fragment];

*out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];

*out++ = '=';

}

*out = '\0';

};

BYTE LMoveBit(int base, int MoveNum)

{

BYTE result=base;

if(MoveNum==0)return 1;

if(MoveNum==1)return MoveNum;

result=base<<(MoveNum-1);

return result;

}

//base64解码 snaix@126.com

BYTE* Base64Decode(char *base64code, DWORD base64length)

{

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 buf[4];

int i,j;

int k;

int l=0;

BYTE temp1[4],temp2;

BYTE *Buffer=new BYTE[base64length*3/4];

memset(Buffer,0,base64length*3/4);

DWORD base64a=(base64length/4)-1;

DWORD base64b=0;

int m_padnum=0;

for(;base64b<base64a+1;base64b++)

{

for(i=0;i<4;i++)

{

buf[i]=*(base64code+(base64b*4)+i);

for(j=0;j<65;j++)

{

if(buf[i]==base64_alphabet[j])

{

temp1[i]=j;

break;

}

}

}

i--;

for(k=1;k<4;k++)

{

if(temp1[i-(k-1)]==64){m_padnum++; continue;}

temp1[i-(k-1)]=temp1[i-(k-1)]/LMoveBit(2,(k-1)*2);

temp2=temp1[i-k];

temp2=temp2&(LMoveBit(2,k*2)-1);

temp2*=LMoveBit(2,8-(2*k));//move 4

temp1[i-(k-1)]=temp1[i-(k-1)]+temp2;

Buffer[base64b*3+(3-k)]=temp1[i-(k-1)];

}

}

return Buffer;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有