How to convert string to hexadecimal in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
   
int main()
{
    unsigned char str[32] = "C Programming", hex[128] = "";
    int j = 0, size = strlen(str);;
  
    for (int i = 0; i < size; i++, j += 3) { 
        sprintf((char*)hex + j, "%0X%c", str[i], ' ');
    }
      
    hex[j] = '\0'; 
       
    printf("%s\n", hex);
  
    return 0;
}
  
  
 
  
/*
run:
  
43 20 50 72 6F 67 72 61 6D 6D 69 6E 67
  
*/

 



answered Sep 18, 2021 by avibootz
edited Nov 18, 2024 by avibootz

Related questions

1 answer 170 views
170 views asked Sep 18, 2021 by avibootz
1 answer 194 views
2 answers 303 views
3 answers 360 views
1 answer 236 views
1 answer 148 views
148 views asked Sep 17, 2021 by avibootz
2 answers 195 views
195 views asked Aug 25, 2021 by avibootz
...