C++ 实现string转BYTE

用于将形如"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     }
34 
35     ++iPos;
36     power *= 16;
37   }
38 
39   return true;
40 }

原文链接: https://www.cnblogs.com/TssiNG-Z/p/12299944.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193035

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午6:14
下一篇 2023年2月12日 下午6:14

相关推荐