转换的ASCII字符[]为十六进制的char []用C [英] Convert ascii char[] to hexadecimal char[] in C

查看:181
本文介绍了转换的ASCII字符[]为十六进制的char []用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个char []在ASCII到char []十六进制的。

事情是这样的:

您好 - > 68656C6C6F

我想通过键盘来读取的字符串。它必须是16个字符。

这是我的code现在。我不知道该怎么做操作。我读到strol但我认为这只是转换海峡号为int的十六进制...

 的#include<&stdio.h中GT;
主要()
{
    INT I = 0;
    字符字[17];    的printf(介绍词:);    与fgets(字,16个,标准输入);
    字[16] ='\\ 0';
    对于(i = 0; I< 16;我++){
        的printf(%C字[I]);
    }
 }

我使用与fgets,因为我读的是比与fgets更好,但如果需要,我可以改变它。

与此相关的

,我想在uint8_t有阵列读取字符串转换,在一个每个加盟2个字节来获得十六进制数。

我有这个功能,我在Arduino的使用了很多,所以我觉得应该在一个正常的C程序是没有问题的。

  uint8_t有* hex_de code(字符*中,为size_t LEN,uint8_t有*总分)
{
    unsigned int类型I,T,HN,LN;    对于(t = 0,I = 0; I&下; len个; I + = 2,+ t)的{            HN =在[I]≥ '9'? (在[I] | 32) - 'A'+ 10:[Ⅰ] - '0';
            LN =在第[i + 1]≥ '9'? (在第[i + 1] | 32) - 'A'+ 10:在第[i + 1] - '0';            出[T] =(HN<&4;)| LN;
            的printf(%S,出[T]);
    }
    返回的;

}

不过,每当我调用该函数在我的code,我得到一个分段错误。

添加此code将第一个答案的code:

  uint8_t有*总分;
    hex_de code(key_DM,sizeof的(out_key),淘汰);

我试图通过所有必要的参数,并得到了中数组我需要什么,但是它无法...


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;诠释主要(无效){
    炭字[17],outword [33]; // 17:16 + 1,33:16 * 2 + 1
    INT I,LEN;    的printf(介绍词:);
    与fgets(字的sizeof(字),标准输入);
    LEN = strlen的(字);
    如果(字[LEN-1] =='\\ n')
        字[ - LEN] ='\\ 0';    对于(i = 0; I< LEN,我++){
        sprintf的(outword + I * 2,%02X,字[I]);
    }
    的printf(%S \\ n,outword);
    返回0;
}

I am trying to convert a char[] in ASCII to char[] in hexadecimal.

Something like this:

hello --> 68656C6C6F

I want to read by keyboard the string. It has to be 16 characters long.

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...

#include <stdio.h>
main()
{
    int i = 0;
    char word[17];

    printf("Intro word:");

    fgets(word, 16, stdin);
    word[16] = '\0';
    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.

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.

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.

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;

}

But, whenever I call that function in my code, I get a segmentation fault.

Adding this code to the code of the first answer:

    uint8_t* out;
    hex_decode(key_DM, sizeof(out_key), out);

I tried to pass all necessary parameters and get in out array what I need but it fails...

解决方案

#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] = '\0';

    for(i = 0; i<len; i++){
        sprintf(outword+i*2, "%02X", word[i]);
    }
    printf("%s\n", outword);
    return 0;
}

这篇关于转换的ASCII字符[]为十六进制的char []用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆