hex - Convert ascii char[] to hexadecimal char[] in C -


i trying convert char[] in ascii char[] in hexadecimal.

something this:

hello --> 68656c6c6f

i want read keyboard string. has 16 characters long.

this code now. don't know how operation. read strol think convert str number int hex...

#include <stdio.h> main() {     int = 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 using fgets because read better fgets can change if necessary.

related this, trying convert string read in uint8_t array, joining each 2 bytes in 1 hex number.

i have function using lot in arduino think should work in normal c program without problems.

uint8_t* hex_decode(char *in, size_t len, uint8_t *out) {     unsigned int i, t, hn, ln;      (t = 0,i = 0; < 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 call function in code, segmentation fault.

adding code code of first answer:

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

i tried pass necessary parameters , in out array need 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; } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -