码迷,mamicode.com
首页 > 其他好文 > 详细

how convert large HEX string to binary array ?

时间:2015-06-26 08:00:31      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

how convert large HEX string to binary

I have a string with 14 characters . This is a hex represantation of 7bytes. I want to convert it to binary.

 

int32_t Hex2Bin( uint8_t * pHexString, uint8_t * pBinArray )
{
  int o = 0;
  int i = 0;

  while ( pHexString[ i ] != 0x00 )
  {
    switch ( pHexString[ i ] )
    {
      case 0:
        pBinArray[ o ] = 0x00;
        break;
      case 1:
        pBinArray[ o ] = 0x10;
        break;
      case 2:
        pBinArray[ o ] = 0x20;
        break;
      case 3:
        pBinArray[ o ] = 0x30;
        break;
      case 4:
        pBinArray[ o ] = 0x40;
        break;
      case 5:
        pBinArray[ o ] = 0x50;
        break;
      case 6:
        pBinArray[ o ] = 0x60;
        break;
      case 7:
        pBinArray[ o ] = 0x70;
        break;
      case 8:
        pBinArray[ o ] = 0x80;
        break;
      case 9:
        pBinArray[ o ] = 0x90;
        break;
      case A:
      case a:
        pBinArray[ o ] = 0xa0;
        break;
      case B:
      case b:
        pBinArray[ o ] = 0xb0;
        break;
      case C:
      case c:
        pBinArray[ o ] = 0xc0;
        break;
      case D:
      case d:
        pBinArray[ o ] = 0xd0;
        break;
      case E:
      case e:
        pBinArray[ o ] = 0xe0;
        break;
      case F:
      case f:
        pBinArray[ o ] = 0xf0;
        break;
      default:
        return -1;
    }

    if ( pHexString[ i + 1 ] == 0x00 )
      return -1;

    switch ( pHexString[ i + 1 ] )
    {
      case 0:
        pBinArray[ o ] |= 0x00;
        break;
      case 1:
        pBinArray[ o ] |= 0x01;
        break;
      case 2:
        pBinArray[ o ] |= 0x02;
        break;
      case 3:
        pBinArray[ o ] |= 0x03;
        break;
      case 4:
        pBinArray[ o ] |= 0x04;
        break;
      case 5:
        pBinArray[ o ] |= 0x05;
        break;
      case 6:
        pBinArray[ o ] |= 0x06;
        break;
      case 7:
        pBinArray[ o ] |= 0x07;
        break;
      case 8:
        pBinArray[ o ] |= 0x08;
        break;
      case 9:
        pBinArray[ o ] |= 0x09;
        break;
      case A:
      case a:
        pBinArray[ o ] |= 0x0a;
        break;
      case B:
      case b:
        pBinArray[ o ] |= 0x0b;
        break;
      case C:
      case c:
        pBinArray[ o ] |= 0x0c;
        break;
      case D:
      case d:
        pBinArray[ o ] |= 0x0d;
        break;
      case E:
      case e:
        pBinArray[ o ] |= 0x0e;
        break;
      case F:
      case f:
        pBinArray[ o ] |= 0x0f;
        break;
      default:
        return -1;
    }

    i += 2;
    o++;
  }

  return o;
}

 

how convert large HEX string to binary array ?

标签:

原文地址:http://www.cnblogs.com/shangdawei/p/4601371.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!