美文网首页
c hex字符串转hex数组

c hex字符串转hex数组

作者: 竹林_b4a5 | 来源:发表于2019-12-06 09:53 被阅读0次
void hexStringToByte(const char *hex_string,size_t hex_len,unsigned char **bytes,size_t *byte_len)
{
    size_t loop,i,out_len=0;
    BYTE *out_bs = NULL;
    int temp=0x10;
    const char limits[] = {'0','9',0,'a','f',0xa,'A','F',0xa};

    if(hex_len == 0){
        return;
    }

    *bytes = calloc((size_t)hex_len,sizeof (BYTE));
    out_bs = *bytes;

    for(loop = 0;loop<hex_len;loop++){
        char hex_char = hex_string[loop];
        for(i=0;i<=6;i+=3){
            if(hex_char >= limits[i] && hex_char <= limits[i+1]){
                temp = temp | (BYTE)(hex_char-limits[i]+limits[i+2]);
                if(temp & 0x100){
                    out_bs[out_len] = temp&0xff;
                    ++out_len;
                    temp = 0x10;
                }else{
                    temp <<= 4;
                }
                break;
            }
        }
    }
    realloc(out_bs,(size_t)out_len);
    *byte_len = out_len;
}

相关文章

网友评论

      本文标题:c hex字符串转hex数组

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