1. 程式人生 > >Qt之base64編碼最簡單程式

Qt之base64編碼最簡單程式

//!!!dev c++

#include <iostream>

#include <stdlib.h>
#include <stdio.h>


//AA9HmgAAAAFZyLXo





unsigned char base64Table[64] = 
{
'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', 

'+', '/',
};








unsigned char orgData[3]={'X', 'u', 'e'};
unsigned char base64Data[256];




unsigned char orgData1[] = "\x00\x0f\x47\x9a\x00\x00\x00\x01\x59\xc8\xb5\xe8";








unsigned char ToBase64(unsigned char *orgData, unsigned char *base64Data)
{
base64Data[0]=orgData[0] >> 2;
base64Data[1]=((orgData[0] & 0x03) << 4) | (orgData[1] >> 4);
base64Data[2]=((orgData[1] & 0x0F) << 2) | ((orgData[2] >> 6) & 0x03);
base64Data[3]=(orgData[2] & 0x3F);

printf("orgData:");
for(int i=0; i<3; i++)
{
printf("%02x", orgData[i]);
}
printf("\n");

printf("base64Data:");
for(int i=0; i<4; i++)
{
printf("%c", base64Table[base64Data[i]]);
}
printf("\n");
}










int main(int argc, char** argv) 
{
//ToBase64(orgData, base64Data);

ToBase64(orgData1, base64Data);
ToBase64(orgData1+3, base64Data+4);
ToBase64(orgData1+6, base64Data+8);
ToBase64(orgData1+9, base64Data+12);

printf(".......base64Data.......:\n");
for(int i=0; i<16; i++)
{
printf("%c", base64Table[base64Data[i]]);
}
printf("\n");
return 0;
}