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

类别:编程语言 点击:0 评论:0 推荐:

// 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解码 [email protected]
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;
}

本文地址:http://com.8s8s.com/it/it26222.htm