前言: 在项目中, 经常遇到需要各种数据之间转换的, 虽然简单但是却没有必要每个都记住. 所以每次都要去网上查询一番, 很是繁琐.于是决定把自己遇到的都记录在这篇文章里(随时遇到随时更新), 方便以后使用.希望对大家也有所帮助!
Base64编码
/**
* @description: Base64编码
* BCB6 C++
* @param1: Data:数据
* @param2: DataLen:数据长度
* @return: AnsiString(字符串)
*/
AnsiString TIDCard::EncodeBase64(const unsigned char* Data,int DataLen)
{
//编码表
const char EncodeTable[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
//返回值
AnsiString strEncode;
unsigned char Tmp[4]={0};
int LineLength=0;
for(int i=0;i<(int)(DataLen / 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;
}
}
//对剩余数据进行编码
int Mod=DataLen % 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;
}
Base64解码
/**
* @description: Base64解码
* BCB6 C++
* @param1: Data:数据
* @param2: DataLen:输入数据长度
* @param3: OutLen:输出数据长度
* @return: AnsiString(字符串)
*/
AnsiString TIDCard::Decode(const char* Data,int DataLen,int& OutLen)
{
//解码表
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'
};
//返回值
AnsiString strDecode;
int nValue;
int i= 0;
while (i < DataLen)
{
if (*Data != '\r' && *Data!='\n')
{
nValue = DecodeTable[*Data++] << 18;
nValue += DecodeTable[*Data++] << 12;
strDecode+=(nValue & 0x00FF0000) >> 16;
OutLen++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++] << 6;
strDecode+=(nValue & 0x0000FF00) >> 8;
OutLen++;
if (*Data != '=')
{
nValue += DecodeTable[*Data++];
strDecode+=nValue & 0x000000FF;
OutLen++;
}
}
i += 4;
}
else// 回车换行,跳过
{
Data++;
i++;
}
}
return strDecode;
}
byte数组转字符串
/**
* @description: byte数组转字符串
* BCB6 C++
* @param1: in:输入数据
* @param2: length:输出数据长度
* @return: AnsiString(字符串)
*/
AnsiString TSamThread::BytetoStr(byte *in, int length)
{
AnsiString sTraceOut = "";
for (int i=0;i
转换卡号 0123456789->1032547698 第一二位对换、三四位对换...
/**
* Object-C(iOS开发)
* @param1: cardNumber:输入的号码
* @return: NSString(字符串)
*/
- (NSString *)dealCardNumber:(NSString *)cardNumber
{
NSInteger cardLength = cardNumber.length;
NSString *cardNum = @"";
for(NSInteger i = 0; i < cardLength; i++) {
if(i%2 == 0) {
cardNum = [cardNum stringByAppendingString:[NSString stringWithFormat:@"%c", [cardNumber characterAtIndex:i + 1]]];
}
else{
cardNum = [cardNum stringByAppendingString:[NSString stringWithFormat:@"%c", [cardNumber characterAtIndex:i - 1]]];
}
}
return cardNum;
}
喜欢的小伙伴点个喜欢或关注吧, 你们的鼓励是我前进的动力!