char[] to hex string exercise

前端 未结 16 839
感情败类
感情败类 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:47

    I assume this is Windows+IA32.
    Try to use short int instead of the two hexadecimal letters.

    short int hex_table[256] = {'0'*256+'0', '1'*256+'0', '2'*256+'0', ..., 'E'*256+'F', 'F'*256+'F'};
    unsigned short int* pszHex = &str[0];
    
    stick = clock();
    
    for (const unsigned char* pChar = _pArray; pChar != pEnd; pChar++) 
        *pszHex++ = hex_table[*pChar];
    
    etick = clock();
    

提交回复
热议问题