由于我采用Qt来写的界面,所以用到了QString类型
1. QString和string的相互转换
[cpp] view plain copy print ?
-
-
-
- QString s2q(const string &s)
- {
- return QString(QString::fromLocal8Bit(s.c_str()));
- }
- string q2s(const QString &s)
- {
- return string((const char *)s.toLocal8Bit());
- }
//------------------------ // QString and string change //------------------------ QString s2q(const string &s) { return QString(QString::fromLocal8Bit(s.c_str())); } string q2s(const QString &s) { return string((const char *)s.toLocal8Bit()); }
2. QString to Char*
[cpp] view plain copy print ?
- QString data;
- data.toLatin1().data();
QString data; data.toLatin1().data();
3. char to number
[cpp] view plain copy print ?
- int char2num(char ch)
- {
- if(ch>='0'&&ch<='9')return ch-'0';
- else if(ch>='a'&&ch<='f')return ch-'a'+10;
- return -1;
- }
int char2num(char ch) { if(ch>='0'&&ch<='9')return ch-'0'; else if(ch>='a'&&ch<='f')return ch-'a'+10; return -1; }
4. number to QString
[css] view plain copy print ?
- QString::number(number,10);//10表示的是进制,如是16进制,则改为16
QString::number(number,10);//10表示的是进制,如是16进制,则改为16
5. uint8_t to QString
[cpp] view plain copy print ?
- uint8_t Message_Digest;
- QString::number(Message_Digest,16);
uint8_t Message_Digest; QString::number(Message_Digest,16);
6. BYTE* to char[] to QString
[cpp] view plain copy print ?
- QString textshow = NULL;
- BYTE * output = new BYTE[1024];
- for(int i=0; i<16; i++)
- {
- char pBuff[2];
- sprintf(pBuff,"%02x",output[i]);
- textshow = textshow + pBuff;
- }
QString textshow = NULL; BYTE * output = new BYTE[1024]; for(int i=0; i<16; i++) { char pBuff[2]; sprintf(pBuff,"%02x",output[i]); textshow = textshow + pBuff; }
7. string to BYTE [] //其中#define unsigned char BYTE
[cpp] view plain copy print ?
-
-
-
- void CryptClient::HexStrToByte(string str_arr, BYTE byte_arr[24])
- {
- unsigned char ch1;
- unsigned char ch2;
- int k = 0;
- for (int i=0; i<str_arr.length()-1; i = i+2)
- {
- ch1 = str_arr.at(i);
- ch2 = str_arr.at(i+1);
- if (ch1>=48 && ch1 <= 57)
- {
- ch1 &= 0x0F;
- }
- if (ch1>='A' && ch1 <='F')
- {
- ch1 &= 0x0F;
- ch1 += 0x09;
- }
- if (ch2 >= 48 && ch2 <= 57)
- {
- ch2 &= 0x0F;
- }
- if (ch2>='A' && ch2 <='F')
- {
- ch2 &= 0x0F;
- ch2 += 0x09;
- }
- ch1<<=4;
- byte_arr[k] = ch1 + ch2;
- k++;
- }
- }
////------------------------------------------- //// 将字符串类型转换为BYTE数组 ////------------------------------------------- void CryptClient::HexStrToByte(string str_arr, BYTE byte_arr[24]) { unsigned char ch1; unsigned char ch2; int k = 0; for (int i=0; i<str_arr.length()-1; i = i+2) { ch1 = str_arr.at(i); ch2 = str_arr.at(i+1); if (ch1>=48 && ch1 <= 57) { ch1 &= 0x0F; } if (ch1>='A' && ch1 <='F') { ch1 &= 0x0F; ch1 += 0x09; } if (ch2 >= 48 && ch2 <= 57) { ch2 &= 0x0F; } if (ch2>='A' && ch2 <='F') { ch2 &= 0x0F; ch2 += 0x09; } ch1<<=4; byte_arr[k] = ch1 + ch2;//int类型转byte类型,有问题 k++; } }
8. BYTE[] to string //上个过程的逆过程
[c-sharp] view plain copy print ?
-
-
-
- string* byteToHexStr(BYTE byte_arr[], int arr_len)
- {
- string* hexstr = new string;
- for(int i=0; i<arr_len; i++)
- {
- char hex1;
- char hex2;
- int value = byte_arr[i];
- int v1 = value/16;
- int v2 = value % 16;
-
- if (v1>=0 && v1<=9)
- {
- hex1 = (char)(48 + v1);
- }
- else
- {
- hex1 = (char)(55 + v1);
- }
-
- if (v2>=0 && v2<=9)
- {
- hex2 = (char)(48 + v2);
- }
- else
- {
- hex2 = (char)(55 + v2);
- }
-
- *hexstr = *hexstr + hex1 + hex2;
- }
- return hexstr;
- }
////------------------------------------------- //// 将BYTE数组转换为字符串类型 ////------------------------------------------- string* byteToHexStr(BYTE byte_arr[], int arr_len) { string* hexstr = new string; for(int i=0; i<arr_len; i++) { char hex1; char hex2; int value = byte_arr[i]; int v1 = value/16; int v2 = value % 16; //将商转换为字母 if (v1>=0 && v1<=9) { hex1 = (char)(48 + v1); } else { hex1 = (char)(55 + v1); } //将余数转成字母 if (v2>=0 && v2<=9) { hex2 = (char)(48 + v2); } else { hex2 = (char)(55 + v2); } //将字母连成一串 *hexstr = *hexstr + hex1 + hex2; } return hexstr; }
9. char* to QString
[cpp] view plain copy print ?
- QString keyall1;
- char keych1[MAX_NUM];
- keyall1 = QString(QLatin1String(keych1));
QString keyall1; char keych1[MAX_NUM]; keyall1 = QString(QLatin1String(keych1));
嗯,基本上程序中就用到了这么多,写出来,留着以后自己参考,要是有人能用到,那就更好啦~~~~~~~~