UUID is converted from 32 text (0-e) characters to 16 binary characters, and the Base64 encoded mutual conversion C ++ source co

xiaoxiao2021-03-06  106

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

#include "stdafx.h" #include #include #include #include #include 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 *) SZUID, "00DE8922A3CB4FEE95FB41BD2AC8F958"); TO64FromBits (OUT, SZUID, 43); Printf ("UUID32 from text to Base64 encoding% S ->% S / N", SZUUID, OUT; STRCPY Char *) in, "ABCDEF12123412341234123456789ABC"); Strcpy ((char *) OUT, "ffffffffffffffffffff"); U32TO16 ((unsigned CHAR *) OUT, (CHAR *) IN); Printf ("UUID32 from text to binary% S ->% S / N", IN, OUT; STRCPY ((char *) in, (char *) OUT; STRCPY ((char *) OUT, "ffffffffffffffffffffffffffffffffffffffffff"); U16TO32 (Unsigned char *) in); Printf ("UUID32 from binary to text% S ->% s / n", in, out ); // STRCPY ((char *) OUT; STRCPY ((char *) OUT, "ffffffffffffffffffffffffffffffff"); TO64FROMBITS (OUT, IN, 16); Printf ("From binary to base64 encoding% S ->% S / N ", IN, OUT; STRCPY (CHAR *) IN, (Char *) OUT; STRCPY ((char *) OUT," ffffffffffffffffffff "); char * sztemp = (char *) Base64Decode ((char *) in, 24); // I don't quite understand, 16 characters * 4/3 = 22 is enough,

However, if you do not enter 24, decoding is not the strcpy ((char *) out, sztemp); delete [] sztemp; printf ("encoded from Base64 to Binary% S ->% S / N", IN, OUT; STRCPY ((charr) *) In, (char *) OUT; struffpy ((char *) OUT, "ffffffffffffffffffffffffffffffffffffffffffffffff"); U16TO32 ((unsigned char *) in); Printf ("UUID32 from binary to text% 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 [] = "Fffffffffffffffffffffffff"; 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 encoding away blog software 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;}} (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 decoded 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', '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

转载请注明原文地址:https://www.9cbs.com/read-100382.html

New Post(0)