2012-02-22 165 views
0

假設我有這些變量,如何將字符轉換爲十六進制存儲在uint8_t形式?

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(這只是一個簡單的文本,如「你好」),以十六進制和保存到ndef_msg? 正如你所看到的ndef_default_msg是十六進制形式。 ndef_msg中的數據也應該是這樣的。

有一點背景,在原程序(source code)中,程序會打開一個文件,獲取數據並將其放入ndef_msg,然後寫入卡片。但我不明白它如何將數據轉換爲十六進制。

我想簡化程序,所以它會直接詢問用戶的文本(​​而不是要求文件)。

+0

你希望利用用戶輸入'ndef_input'並保存輸入的字符的ASCII值進入'ndef_msg',還是我誤解了你?或者用戶應該輸入實際的十六進制值? – Dervall 2012-02-22 14:03:32

+3

你知道數據是以二進制形式存儲在內存中的,而且你已經擁有用戶輸入的每個字符的十六進制「版本」? – Eregrith 2012-02-22 14:07:46

+0

@Dervall,的確這就是我打算做的。用戶只需鍵入簡單的文本,如「hello」,然後程序必須轉換爲十六進制值「hello」。 – 2012-02-22 14:35:19

回答

4

爲什麼不直接讀入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,這只是爲了說明我的觀點。

+0

意思是用戶的字符輸入直接傳遞給'ndef_msg'?可能?我怎樣才能做到這一點? – 2012-02-22 15:37:50

+0

據推測,你有空間分配來保存字符,因爲你的情況ndef_msg只是一個指針。或者我誤解了你在做什麼。我想說的是唯一不同的是你的類型的符號,不需要轉換爲十六進制。 – foo 2012-02-22 15:40:08

+0

我明白了。這就是爲什麼我在原始程序中看不到十六進制轉換的原因。我也會嘗試你的解決方案。謝謝。 – 2012-02-22 15:54:50

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; 
+0

哦,'sscanf',我一直在嘗試導致崩潰的'sprintf'。但我認爲'new'是C++?我應該施加'temp_int'而不是(在'uint8_t'類型)? – 2012-02-22 14:50:13

+0

@TyronMaples是的,在C中,它應該是'malloc(sizeof(uint8_t))'。是的。 '* ndef_msg =(uint8_t)temp_int;' – 2012-02-22 14:58:05

+0

感謝您的澄清。一個問題,由於錯誤檢查,轉換將在'if else'語句內完成,所以如果我把'int temp_int;'放在'if else'中,'ndef_msg'在'if else'之後將會有空數據,因爲' temp_int'已被銷燬,不是嗎? – 2012-02-22 15:08:33

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; 
} 

我用這樣的不優雅的解決方案(彩色映射如)一對夫婦的時候,它的工作同樣好其他花哨的解決方案

-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; 
} 
相關問題