參考第二個答案的問題:How to convert from ASCII to Hex and vice versa?Ç - 商店等值的十六進制的字符
我想保存字符六角[3]不同的字符等效如下:
char *str ="abcd";
// I want to get hex[3] of each character in above string and save into the following:
char str2[4]; // should contain hex values as : \x61 for a,\x62 for b,\x63 for c,\x64 for d
我該怎麼做?
我試過到目前爲止以下:
int i;
char ch;
char hex[3];
for(i=0; i<strlen(str);i++) {
ch = charToHex(*(str+i), hex);
// now hex contains the first and second hex characters in hex[0] & hex[1]
// I need to save them in the first index of str2
// e.g. if hex[0] = 7 and hex[1] = f, then str2[0] should be "\x7f"
// -> how do I do this part ?
}
感謝。
這不是一個寫我的代碼爲我的網站。你有什麼嘗試?你卡在哪裏?還要注意,由於空終止符,字符串'「\\ x61」'可能不適合'char [4]'。 (我說「可能」,因爲如果你願意,你可以不使用空終止符而工作,但它是尷尬的,而且,我懷疑,不是你想要的。) – 2012-03-25 02:22:04
答案不會以固定順序出現 - 順序依賴於upvotes和downvotes - 所以「第二個答案」是不是一個有用的描述你所指的。爲什麼不直接鏈接到你想要的答案,使用底部的「鏈接」鏈接? – ruakh 2012-03-25 02:24:26
@MarceloCantos請參閱更新後的問題 – Jake 2012-03-25 02:28:23