Home » C programming language

Convert ASCII string (char[]) to hexadecimal string (char[]) in C

C | Convert ASCII string to hexadecimal string: Here, we are going to learn how to convert a given string (that contains ascii characters) to its equivalent hexadecimal string in C?
Submitted by IncludeHelp, on February 13, 2019

Given an ASCII string (char[]) and we have to convert it into Hexadecimal string (char[]) in C.

Logic:

To convert an ASCII string to hex string, follow below-mentioned steps:

  • Extract characters from the input string and convert the character in hexadecimal format using %02X format specifier, %02X gives 0 padded two bytes hexadecimal value of any value (like int, char).
  • Add these two bytes (characters) which is a hex value of an ASCII character to the output string.
  • After each iteration increase the input string's loop counter (loop) by 1 and output string's loop counter (i) by 2.
  • At the end of the loop, insert a NULL character to the output string.

Example:

    Input: "Hello world!"
    Output: "48656C6C6F20776F726C6421"

C program to convert ASCII char[] to hexadecimal char[]

In this example, ascii_str is an input string that contains "Hello world!", we are converting it to a hexadecimal string. Here, we created a function void string2hexString(char* input, char* output), to convert ASCII string to hex string, the final output string is storing in hex_str variable.

#include <stdio.h>
#include <string.h>

//function to convert ascii char[] to hex-string (char[])
void string2hexString(char* input, char* output)
{
    int loop;
    int i; 
    
    i=0;
    loop=0;
    
    while(input[loop] != '\0')
    {
        sprintf((char*)(output+i),"%02X", input[loop]);
        loop+=1;
        i+=2;
    }
    //insert NULL at the end of the output string
    output[i++] = '\0';
}

int main(){
    char ascii_str[] = "Hello world!";
    //declare output string with double size of input string
    //because each character of input string will be converted
    //in 2 bytes
    int len = strlen(ascii_str);
    char hex_str[(len*2)+1];
    
    //converting ascii string to hex string
    string2hexString(ascii_str, hex_str);
    
    printf("ascii_str: %s\n", ascii_str);
    printf("hex_str: %s\n", hex_str);
    
    return 0;
}

Output

ascii_str: Hello world!
hex_str: 48656C6C6F20776F726C6421

Read more...






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.