char[] to hex string exercise

前端 未结 16 750
感情败类
感情败类 2021-01-12 19:14

Below is my current char* to hex string function. I wrote it as an exercise in bit manipulation. It takes ~7ms on a AMD Athlon MP 2800+ to hexify a 10 million byte array. Is

16条回答
  •  南笙
    南笙 (楼主)
    2021-01-12 19:49

    At the cost of more memory you can create a full 256-entry table of the hex codes:

    static const char _hex2asciiU_value[256][2] =
        { {'0','0'}, {'0','1'}, /* ..., */ {'F','E'},{'F','F'} };
    

    Then direct index into the table, no bit fiddling required.

    const char *pHexVal = pHex[*pChar];
    pszHex[0] = pHexVal[0];
    pszHex[1] = pHexVal[1];
    

提交回复
热议问题