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 159 views
159 views asked Sep 18, 2021 by avibootz
1 answer 180 views
2 answers 284 views
3 answers 343 views
1 answer 226 views
1 answer 137 views
137 views asked Sep 17, 2021 by avibootz
2 answers 181 views
181 views asked Aug 25, 2021 by avibootz
...