> {0x12, 0x34, 0x56, 0xAB, 0xCD}...">
美文网首页
字符串(数字)转成数组

字符串(数字)转成数组

作者: sailture | 来源:发表于2017-12-31 18:42 被阅读0次

VS2017

eg. "123456ABCD"  >> {0x12, 0x34, 0x56, 0xAB, 0xCD}

void StringToHexArray(char *pString, unsigned char *pHexArray)

{

     unsigned char i = 0;

     char strTemp[512] = { '0' };

     int iLength = strlen(pString);

     if (iLength % 2)

     {

         strncat_s(strTemp, pString, sizeof(strTemp) - 2);

         iLength += 1;

     }

     else

     {

         memset(strTemp, 0x00, sizeof(strTemp));

         strncpy_s(strTemp, pString, sizeof(strTemp)-1);

     }

     char strMid[3] = { 0 };

     for (i = 0; i<iLength / 2; i++)

    {

        strncpy_s(strMid, strTemp+i*2, 2);

        sscanf_s(strMid,"%hhX",&pHexArray[i]);

    }

}

相关文章

网友评论

      本文标题:字符串(数字)转成数组

      本文链接:https://www.haomeiwen.com/subject/doeagxtx.html