TssiNG-Z

用于将形如"0x1A"的string转成BYTE类型

代码如下, 有问题欢迎指出

 1 bool str2byte(const std::string &str, BYTE &bRet)
 2 {
 3   bRet = 0x00;       //结果
 4   size_t iPos = 1;   //
 5   size_t power = 1;  //幂次
 6 
 7   //没找的'x'返回
 8   if(std::string::npos == str.find("x"))
 9   {
10     return false;
11   }
12 
13   //从右往左依次取每个字符
14   while (str.find("x") != (str.length()-iPos))
15   {
16     char cVal = str[str.length()-iPos];
17     int iVal = int(cVal);
18 
19     //0~9
20     if ((iVal >= 48) && (iVal <= 57))
21     {
22       bRet += ((iVal-48) * power);
23     }
24     //A~F
25     else if ((iVal >= 65) && (iVal <= 70))
26     {
27       bRet += ((iVal-55) * power);
28     }
29     //a~f
30     else if ((iVal >= 97) && (iVal <= 102))
31     {
32       bRet += ((iVal-87) * power);
33       break;
34     }
35 
36     ++iPos;
37     power *= 16;
38   }
39 
40   return true;
41 }

 

分类:

技术点:

相关文章: