字符串(数字)转成数组

2017-12-31  本文已影响0人  sailture

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]);

    }

}

上一篇 下一篇

猜你喜欢

热点阅读