字符串字符显示与16进制显示之间的相互转换



 
 //Function to convert unsigned char to string of length 2
  void Char2Hex(unsigned char ch, char* szHex)
  {
  unsigned char byte[2];
  byte[0] = ch/16;
  byte[1] = ch%16;
  for(int i=0; i<2; i++)
  {
  if(byte[i] >= 0 && byte[i] <= 9)
  szHex[i] = '0' + byte[i];
  else
  szHex[i] = 'A' + byte[i] - 10;
  }
  szHex[2] = 0;
  }






  //Function to convert string of length 2 to unsigned char
  void Hex2Char(char const* szHex, unsigned char& rch)
  {
  rch = 0;
  for(int i=0; i<2; i++)
  {
  if(*(szHex + i) >='0' && *(szHex + i) <= '9')
  rch = (rch << 4) + (*(szHex + i) - '0');
  else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
  rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
  else
  break;
  }
  }






  //Function to convert string of unsigned chars to string of chars
  void CharStr2HexStr(unsigned char const* pucCharStr, char* pszHexStr, int iSize)
  {
  int i;
  char szHex[3];
  pszHexStr[0] = 0;
  for(i=0; i<iSize; i++)
  {
  Char2Hex(pucCharStr[i], szHex);
  strcat(pszHexStr, szHex);
  }
  }






  //Function to convert string of chars to string of unsigned chars
  void HexStr2CharStr(char const* pszHexStr, unsigned char* pucCharStr, int iSize)
  {
  int i;
  unsigned char ch;
  for(i=0; i<iSize; i++)
  {
  Hex2Char(pszHexStr+2*i, ch);
  pucCharStr[i] = ch;
  }
  } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值