byte与string之间的相互转换

时间:2022-04-27
本文章向大家介绍byte与string之间的相互转换,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

实现unsigned char 数组与string之间的相互转换

 1: #include <iostream>
 2: #include <string>
 3: #include <stdlib.h>
 4: 
 5: using namespace std;
 6: typedef unsigned char byte;
 7: 
 8: ////-------------------------------------------
 9: //// 将字符串类型转换为BYTE数组
 10: ////-------------------------------------------
 11: byte * HexStrToByte(string str_arr, byte byte_arr[])
 12: {
 13:     unsigned char ch1;
 14:     unsigned char ch2;
 15:     byte mybyte[str_arr.length() / 2];
 16:     int k = 0;
 17:     for (int i=0; i<str_arr.length(); i = i+2)
 18:     {
 19:         ch1 = str_arr.at(i);
 20:         ch2 = str_arr.at(i+1);
 21:         if (ch1>=48 && ch1 <= 57)
 22:         {
 23:             ch1 &= 0x0F;
 24:         }
 25:         if (ch1>='A' && ch1 <='F')
 26:         {
 27:             ch1 &= 0x0F;
 28:             ch1 += 0x09;
 29:         }
 30:         if (ch2 >= 48 && ch2 <= 57)
 31:         {
 32:             ch2 &= 0x0F;
 33:         }
 34:         if (ch2>='A' && ch2 <='F')
 35:         {
 36:             ch2 &= 0x0F;
 37:             ch2 += 0x09;
 38:         }
 39:         ch1<<=4;
 40:         byte_arr[k] =(byte)(ch1 + ch2);//int类型转byte类型,有问题
 41:         mybyte[k] = (byte)(ch1 + ch2);
 42:
 43:         k++;
 44:     }
 45:     return mybyte;
 46: }
 47: 
 48: ////-------------------------------------------
 49: //// 将BYTE数组转换为字符串类型
 50: ////-------------------------------------------
 51: string* byteToHexStr(byte byte_arr[], int arr_len)
 52: {
 53:     string* hexstr = new string;
 54:     for(int i=0; i<arr_len; i++)
 55:     {
 56:         char hex1;
 57:         char hex2;
 58:         int value = byte_arr[i];
 59:         int v1 = value/16;
 60:         int v2 = value % 16;
 61:         //将商转换为字母
 62:         if (v1>=0 && v1<=9)
 63:         {
 64:             hex1 = (char)(48 + v1);
 65:         }
 66:         else
 67:         {
 68:             hex1 = (char)(55 + v1);
 69:         }
 70:         //将余数转成字母
 71:         if (v2>=0 && v2<=9)
 72:         {
 73:             hex2 = (char)(48 + v2);
 74:         }
 75:         else
 76:         {
 77:             hex2 = (char)(55 + v2);
 78:         }
 79:         //将字母连成一串
 80:         *hexstr = *hexstr + hex1 + hex2;
 81:     }
 82:     return hexstr;
 83: }
 84: 
 85: int main()
 86: {
 87:     int size = 128;
 88:     byte temp[size];
 89:     int len = sizeof(temp) / sizeof(byte);
 90:     cout << "The length is : " << len << endl;
 91:     cout << "The temp is : " << temp << endl << endl;
 92:     cout << "-------------------" << endl;
 93:
 94:     for(int i = 0; i < len; i++)
 95:     {
 96:         temp[i] = i+1;
 97:     }
 98:
 99:     cout << temp << endl;
 100:     cout << "----------------------" << endl;
 101:     byte test[] =  {0x01, 0x01, 0x02, 0xe4, 0x71, 0x2b, 0x5f, 0x30};
 102:     cout << test << endl;
 103:     for(int i = 0; i < 8; i++)
 104:         cout << test[i] << " ";
 105:     cout << endl;
 106:     cout << "----------------------" << endl;
 107:
 108:
 109:     byte b[]={104,1,2,3,4,5,6,104,81,0,10,6,10,17,10,0,6,10,17,10,0,150,22};
 110:     //string s=System.Text.Encoding.Default.GetString(b);
 111:     int size1 = sizeof(b) / sizeof(byte);
 112:     string str = * byteToHexStr(b, size1);
 113:
 114:     cout << "The string length is : " << str.length() << endl;
 115:     cout << "The byte length is : " << sizeof(b)/sizeof(byte) << endl;
 116:     cout << "The string is : " << str << endl;
 117:
 118:     byte bs[size];
 119:     byte * bs2 = HexStrToByte(str, bs);
 120:     for(int i = 0; i < 23; i++)
 121:         cout << (int) bs[i] ;
 122:     cout << endl;
 123:
 124:     //byte bs[]=System.Text.Encoding.Default.GetBytes(s);
 125:     if(b == bs2)
 126:         cout << "The two byte are the same!" << endl;
 127:     else cout << "The two byte are not the same!" << endl;
 128:
 129:
 130:     return 0;
 131: }