C语言 在 C 中将 ascii char[] 转换为十六进制 char[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16519056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert ascii char[] to hexadecimal char[] in C
提问by Biribu
I am trying to convert a char[] in ASCII to char[] in hexadecimal.
我正在尝试将 ASCII 中的 char[] 转换为十六进制的 char[]。
Something like this:
像这样的东西:
hello --> 68656C6C6F
你好 --> 68656C6C6F
I want to read by keyboard the string. It has to be 16 characters long.
我想通过键盘读取字符串。它必须是 16 个字符长。
This is my code now. I don't know how to do that operation. I read about strol but I think it just convert str number to int hex...
这是我现在的代码。我不知道怎么做那个手术。我读过 strol 但我认为它只是将 str 数字转换为 int hex ...
#include <stdio.h>
main()
{
int i = 0;
char word[17];
printf("Intro word:");
fgets(word, 16, stdin);
word[16] = 'uint8_t* hex_decode(char *in, size_t len, uint8_t *out)
{
unsigned int i, t, hn, ln;
for (t = 0,i = 0; i < len; i+=2,++t) {
hn = in[i] > '9' ? (in[i]|32) - 'a' + 10 : in[i] - '0';
ln = in[i+1] > '9' ? (in[i+1]|32) - 'a' + 10 : in[i+1] - '0';
out[t] = (hn << 4 ) | ln;
printf("%s",out[t]);
}
return out;
';
for(i = 0; i<16; i++){
printf("%c",word[i]);
}
}
I am using fgets because I read is better than fgets but I can change it if necessary.
我使用 fgets 是因为我阅读比 fgets 好,但我可以在必要时更改它。
Related to this, I am trying to convert the string read in a uint8_t array, joining each 2 bytes in one to get the hex number.
与此相关,我正在尝试转换在 uint8_t 数组中读取的字符串,将每 2 个字节合并为一个以获得十六进制数。
I have this function which I am using a lot in arduino so I think it should work in a normal C program without problems.
我有这个函数,我在 arduino 中经常使用它,所以我认为它应该可以在普通的 C 程序中正常工作。
uint8_t* out;
hex_decode(key_DM, sizeof(out_key), out);
}
}
But, whenever I call that function in my code, I get a segmentation fault.
但是,每当我在代码中调用该函数时,都会出现分段错误。
Adding this code to the code of the first answer:
将此代码添加到第一个答案的代码中:
#include <stdio.h>
#include <string.h>
int main(void){
char word[17], outword[33];//17:16+1, 33:16*2+1
int i, len;
printf("Intro word:");
fgets(word, sizeof(word), stdin);
len = strlen(word);
if(word[len-1]=='\n')
word[--len] = 'printf("%c",word[i]);
';
for(i = 0; i<len; i++){
sprintf(outword+i*2, "%02X", word[i]);
}
printf("%s\n", outword);
return 0;
}
I tried to pass all necessary parameters and get in out array what I need but it fails...
我试图传递所有必要的参数并输入我需要的数组,但它失败了......
采纳答案by BLUEPIXY
printf("%02X",word[i]);
回答by MOHAMED
replace this
替换这个
printf("%02X",word[i]);
by
经过
void atoh(char *ascii_ptr, char *hex_ptr,int len)
{
int i;
for(i = 0; i < (len / 2); i++)
{
*(hex_ptr+i) = (*(ascii_ptr+(2*i)) <= '9') ? ((*(ascii_ptr+(2*i)) - '0') * 16 ) : (((*(ascii_ptr+(2*i)) - 'A') + 10) << 4);
*(hex_ptr+i) |= (*(ascii_ptr+(2*i)+1) <= '9') ? (*(ascii_ptr+(2*i)+1) - '0') : (*(ascii_ptr+(2*i)+1) - 'A' + 10);
}
}
回答by Bart Friederichs
Use the %02Xformat parameter:
使用%02X格式参数:
More info can be found here: http://www.cplusplus.com/reference/cstdio/printf/
更多信息可以在这里找到:http: //www.cplusplus.com/reference/cstdio/printf/

