如何将char转换为以uint8_t形式存储的十六进制?

时间:2012-02-22 13:59:51

标签: c hex uint

假设我有这些变量,

const uint8_t ndef_default_msg[33] = {
    0xd1, 0x02, 0x1c, 0x53, 0x70, 0x91, 0x01, 0x09,
    0x54, 0x02, 0x65, 0x6e, 0x4c, 0x69, 0x62, 0x6e,
    0x66, 0x63, 0x51, 0x01, 0x0b, 0x55, 0x03, 0x6c,
    0x69, 0x62, 0x6e, 0x66, 0x63, 0x2e, 0x6f, 0x72,
    0x67
};
uint8_t *ndef_msg;
char *ndef_input = NULL;

如何将ndef_input(只是纯文本,如“hello”)转换为十六进制并保存到ndef_msg? 正如您所看到的,ndef_default_msg是十六进制形式。 ndef_msg内的数据也应该是这样的。

在原始程序(source code)中有一些背景知识,程序将打开一个文件,获取数据并将其放入ndef_msg内,然后将其写入卡中。但我不明白它如何获取数据并转换为十六进制。

我想简化程序,以便直接向用户询问文本(而不是要求提供文件)。

5 个答案:

答案 0 :(得分:6)

为什么不直接将它读入ndef_msg,(如果假设它是一个纯数组,则减去\ 0)。十六进制只是用于演示,你可以选择十进制或八进制,但内容没有任何后果。

void print_hex(uint8_t *s, size_t len) {
    for(int i = 0; i < len; i++) {
        printf("0x%02x, ", s[i]);
    }
    printf("\n");
}

int main()
{
    uint8_t ndef_msg[34] = {0};

    scanf("%33s", ndef_msg);
    print_hex(ndef_msg, strlen((char*)ndef_msg));


    return 0;
}

您可能需要以不同的方式处理字符串的读取以允许空白和或许忽略\0,这只是为了说明我的观点。

答案 1 :(得分:0)

如果我理解正确,您会读取存储在ndef_input中的十六进制格式的数据,解析它并将值保存在ndef_msg中。

你可以使用

// parse the hex string and store it in an int variable
int temp_int;
sscanf(ndef_input, "%x", &temp_int);
// covert it to uint8_t type
ndef_msg = malloc(sizeof(uint8_t));
*ndef_msg = (uint8_t)temp_int;

答案 2 :(得分:0)

可能不是很优雅但很简单:定义一个将字符代码(0到255)映射到所需值的查找表。

// something like this:
for( i = 0; i < INPUT_LEN; ++i ) {
    value_in = input[i];
    value_out = lut[value_in];
    array_out[i] = value_out;
}

我曾多次使用这种不优雅的解决方案(例如用于颜色映射),并且它与其他花哨的解决方案同样有效

答案 3 :(得分:0)

char *ndef_input="Z";

uint8_t b=90; //assume this is your character Z in decimal ascii code 90 and HEX = 5A
uint8_t LSB = b & 0x0F; // this is LSB 10 decimal = A
uint8_t MSB = (b & 0xF0)>>4; // this is MSB 5 in decimal = 5 in Hex
cout << "MSB" << MSB << "LSB" << LSB;

答案 4 :(得分:-1)

我希望能帮到你

/*
 *  DESCRIPTION
 *    Converts a block in ASCII representation to binary
 *  PARAMETERS
 *    char * inMessage      : message in ASCII format, '\0' terminated
 *  OUTPUTS
 *    uint8 * outMessage    : output message in binary format
 *                    Format: outMessage[i], where i is byte number
 *  RETURN VALUE
 *    uint32                : number of converted bytes
 */
uint32 ascii2hex_block( uint8 * outMessage, int32 out_len, const char * inMessage )
{
    #define SET_BIT(U,N)   ((U) |=   (0x1 << (N)))

    int32   i = 0;
    int32   k = 0;
    int32   blockLen = 0;
    char    inChar;
    uint8   hexVal;
    uint32  retVal = 0;

    while ( inMessage[blockLen]!='\0' )     blockLen++;
    blockLen = blockLen >> 1;

    if (blockLen <= out_len)                        // not enough space in output
    {
        retVal = blockLen;
        for (i = 0; i < blockLen; i++)
        {
            outMessage[i] = 0;
            inChar = inMessage[k];
            hexVal = ascii2hex( inChar );
            if (hexVal == 0xff) retVal = 0;     // found an invalid character
            if ( (hexVal & (0x1 << 0) ) != 0 )  SET_BIT( outMessage[i], 4 );
            if ( (hexVal & (0x1 << 1) ) != 0 )  SET_BIT( outMessage[i], 5 );
            if ( (hexVal & (0x1 << 2) ) != 0 )  SET_BIT( outMessage[i], 6 );
            if ( (hexVal & (0x1 << 3) ) != 0 )  HELPER_SET_BIT( outMessage[i], 7 );
            k++;
            inChar = inMessage[k];
            hexVal = ascii2hex( inChar );
            if ( (hexVal & (0x1 << 0) ) != 0 )  SET_BIT( outMessage[i], 0 );
            if ( (hexVal & (0x1 << 1) ) != 0 )  SET_BIT( outMessage[i], 1 );
            if ( (hexVal & (0x1 << 2) ) != 0 )  SET_BIT( outMessage[i], 2 );
            if ( (hexVal & (0x1 << 3) ) != 0 )  SET_BIT( outMessage[i], 3 );
            k++;
        }
    }   

    return retVal;
}

ascii2hex的定义如下:

/*
 *  DESCRIPTION
 *    Converts an ascii character ('0'..'f' or '0'..'F') to corresponding integer value.
 *    In case of invalid ascii character, return value is 0xff
 *  USAGE
 *    uint8 ascii2hex( char inASCII );
 *  PARAMETERS
 *    char inASCII  : ascii character to convert
 *  RETURN VALUE
 *    uint8         : value of inASCII, 0xff for invalid input
 */
uint8 ascii2hex( char inASCII )
{
    uint8 retHex=0xff;

    if( (inASCII>=48) && (inASCII<=57) )
        retHex = inASCII-48;
    else if( (inASCII>=65) && (inASCII<=70) )
        retHex = inASCII-55;
    else if( (inASCII>=97) && (inASCII<=102) )
        retHex = inASCII-87;

    return retHex;
}