`
byytj
  • 浏览: 48740 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

111111

阅读更多
  1. // t4.cpp : Defines the entry point for the console application.   
  2. //   
  3.   
  4. #include "stdafx.h"   
  5.   
  6. #include <string>   
  7. #include <iostream>   
  8. #include <cctype>   
  9. #include <algorithm>   
  10.   
  11. /* 
  12. 入口参数:pSrc  源十六进制数据 
  13. 出口参数:dest  存放运算结果 
  14.     返回:true  转换成功 
  15.           false 失败 
  16. */  
  17. bool Hex2String(unsigned char *pSrc,std::string &dest,int nL)  
  18. {  
  19.     char buf[256];  
  20.       
  21.     memset((char *)buf,0,sizeof(buf));  
  22.   
  23.     unsigned char hb;  
  24.     unsigned char lb;  
  25.   
  26.     for(int i=0;i<nL;i++)  
  27.     {  
  28.         hb=(pSrc[i]&0xf0)>>4;  
  29.   
  30.         if( hb>=0 && hb<=9 )  
  31.             hb += 0x30;  
  32.         else if( hb>=10 &&hb <=15 )  
  33.             hb = hb -10 + 'A';  
  34.         else  
  35.             return false;  
  36.   
  37.         lb = pSrc[i]&0x0f;  
  38.         if( lb>=0 && lb<=9 )  
  39.             lb += 0x30;  
  40.         else if( lb>=10 && lb<=15 )  
  41.             lb = lb - 10 + 'A';  
  42.         else  
  43.             return false;  
  44.   
  45.         buf[i*2]   = hb;  
  46.         buf[i*2+1] = lb;  
  47.     }  
  48.     dest = buf;  
  49.     return true;  
  50. }  
  51.   
  52. /* 
  53. 入口参数:src  源字符串 
  54. 出口参数:dest  存放运算结果 
  55.     返回:true  转换成功 
  56.           false 失败 
  57. */  
  58. bool String2Hex(std::string &src,unsigned char *dest)  
  59. {  
  60.     unsigned char hb;  
  61.     unsigned char lb;  
  62.   
  63.     if(src.size()%2!=0)  
  64.         return false;  
  65.   
  66.     transform(src.begin(), src.end(), src.begin(), toupper);  
  67.   
  68.     for(int i=0, j=0;i<src.size();i++)  
  69.     {  
  70.         hb=src[i];  
  71.         if( hb>='A' && hb<='F' )  
  72.             hb = hb - 'A' + 10;  
  73.         else if( hb>='0' && hb<='9' )  
  74.             hb = hb - '0';  
  75.         else  
  76.             return false;  
  77.   
  78.         i++;  
  79.         lb=src[i];  
  80.         if( lb>='A' && lb<='F' )  
  81.             lb = lb - 'A' + 10;  
  82.         else if( lb>='0' && lb<='9' )  
  83.             lb = lb - '0';  
  84.         else  
  85.             return false;  
  86.   
  87.         dest[j++]=(hb<<4)|(lb);  
  88.     }  
  89.     return true;  
  90. }  
  91.   
  92. //下面是使用举例,在VisualStudio2008+SP1中调试通过   
  93. int _tmain(int argc, _TCHAR* argv[])  
  94. {  
  95.     unsigned char srcB[]={0x12,0x34,0x56,0x78,0x90,0xab,0xbc,0xcd,0xde,0xef};  
  96.   
  97.     std::string strDest;  
  98.   
  99.     Hex2String(srcB,strDest,sizeof(srcB));  
  100.     std::cout<<"HexToString:"<<strDest<<std::endl;  
  101.   
  102.     if(String2Hex(strDest,srcB))  
  103.         std::cout<<"StringToHex:Success!"<<std::endl;  
  104.     else  
  105.         std::cout<<"StringToHex:Failed!"<<std::endl;  
  106.   
  107.     return 0;  
  108. }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics