Open In App

Convert a string to hexadecimal ASCII values

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string as input, write a program to convert the characters of the given string into the hexadecimal equivalent of ASCII values. 

Examples : 

Input: Geek
Output: 4765656b

Input: IronMan part 3
Output :49726f6e4d616e20706172742033

ASCII stands for American Standard Code for Information Interchange. ASCII is a standard that assigns letters, numbers, and other characters within the 256 slots available in the 8-bit code. E.g the lowercase “h” character (Char) has a decimal value of 104, which is “01101000” in binary and “68” in hexadecimal.

The “Hexadecimal” or simply “Hex” numbering system uses the Base of 16 system. The hexadecimal number uses 16 symbols {0, 1, 2, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F} to represent all numbers. Here, (A, B, C, D, E, F) represents (10, 11, 12, 13, 14, 15). 

Algorithm : 

  1. Initialize final Hex string as empty.
  2. Consider every character from input, cast it into integer. This integer value is ascii value of that character.
  3. Change this integer value into hexadecimal value and add this hexadecimal value to final Hex string.

Basic implementation of the above idea:  

C++




// C++ program to convert
// ASCII string to Hexadecimal
// format string
// Function to convert
// decimal to hexadecimal
 
#include <iostream>
 
using namespace std;
 
// function to convert decimal to hexadecimal
string decToHexa(int n)
{
    // char array to store hexadecimal number
    char hexaDeciNum[100];
 
    // counter for hexadecimal number array
    int i = 0;
    while (n != 0) {
        // temporary variable to store remainder
        int temp = 0;
 
        // storing remainder in temp variable.
        temp = n % 16;
 
        // check if temp < 10
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
 
        n = n / 16;
    }
 
    string ans = "";
 
    // printing hexadecimal number array in reverse order
    for (int j = i - 1; j >= 0; j--)
        ans += hexaDeciNum[j];
 
    return ans;
}
// Function to convert ASCII to HEX
string ASCIItoHEX(string ascii)
{
    // Initialize final String
    string hex = "";
 
    // Make a loop to iterate through
    // every character of ascii string
    for (int i = 0; i < ascii.length(); i++) {
        // Take a char from
        // position i of string
        char ch = ascii[i];
 
        // Cast char to integer and
        // find its ascii value
        int tmp = (int)ch;
 
        // Change this ascii value
        // integer to hexadecimal value
        string part = decToHexa(tmp);
 
        // Add this hexadecimal value
        // to final string.
        hex += part;
    }
 
    // Return the final
    // string hex
    return hex;
}
int main()
{
 
    // Driver code
    // Print the Hex String
    cout << (ASCIItoHEX("Geek"));
}
 
// this code is contributed by phasing17


Java




// Java program to convert ASCII
// string to Hexadecimal format string
import java.util.Scanner;
 
class ASCIItoHEX {
 
    // function to convert ASCII to HEX
    public static String ASCIItoHEX(String ascii)
    {
       
        // Initialize final String
        String hex = "";
 
        // Make a loop to iterate through
        // every character of ascii string
        for (int i = 0; i < ascii.length(); i++) {
 
            // take a char from
            // position i of string
            char ch = ascii.charAt(i);
 
            // cast char to integer and
            // find its ascii value
            int in = (int)ch;
 
            // change this ascii value
            // integer to hexadecimal value
            String part = Integer.toHexString(in);
 
            // add this hexadecimal value
            // to final string.
            hex += part;
        }
       
        // return the final string hex
        return hex;
    }
 
    // Driver Function
    public static void main(String arg[])
    {
        // print the Hex String
        System.out.println(ASCIItoHEX("Geek"));
    }
}


Python3




# Python3 program to convert ASCII
# string to Hexadecimal format string
 
# function to convert ASCII to HEX
def ASCIItoHEX(ascii):
 
    # Initialize final String
    hexa = ""
 
    # Make a loop to iterate through
    # every character of ascii string
    for i in range(len(ascii)):
 
        # take a char from
        # position i of string
        ch = ascii[i]
 
        # cast char to integer and
        # find its ascii value
        in1 = ord(ch)
   
        # change this ascii value
        # integer to hexadecimal value
        part = hex(in1).lstrip("0x").rstrip("L")
 
        # add this hexadecimal value
        # to final string.
        hexa += part
 
    # return the final string hex
    return hexa
   
# Driver Function
if __name__ == '__main__':
 
    # print the Hex String
    print(ASCIItoHEX("Geek"))
 
# This code is contributed by pratham76


C#




// C# program to convert
// ASCII string to Hexadecimal
// format string
using System;
class GFG{
     
// Function to convert
// decimal to hexadecimal
static string decToHexa(int n)
{   
  // char array to store
  // hexadecimal number
  char []hexaDeciNum =
         new char[100];
 
  // counter for hexadecimal
  // number array
  int i = 0;
   
  while(n != 0)
  {   
    // temporary variable
    // to store remainder
    int temp  = 0;
 
    // Storing remainder
    // in temp variable.
    temp = n % 16;
 
    // Check if temp < 10
    if(temp < 10)
    {
      hexaDeciNum[i] =
          (char)(temp + 48);
      i++;
    }
    else
    {
      hexaDeciNum[i] =
          (char)(temp + 87);
      i++;
    }
 
    n = n / 16;
  }
 
  string ans = "";
 
  // Printing hexadecimal number
  // array in reverse order
  for(int j = i - 1;
          j >= 0; j--)
  {
    ans += hexaDeciNum[j];
  }
 
  return ans;
}  
 
// Function to convert ASCII to HEX
public static string ASCIItoHEX(string ascii)
{
  // Initialize final String
  string hex = "";
 
  // Make a loop to iterate through
  // every character of ascii string
  for (int i = 0;
           i < ascii.Length; i++)
  {
    // Take a char from
    // position i of string
    char ch = ascii[i];
 
    // Cast char to integer and
    // find its ascii value
    int tmp = (int)ch;
 
    // Change this ascii value
    // integer to hexadecimal value
    string part = decToHexa(tmp);
 
    // Add this hexadecimal value
    // to final string.
    hex += part;
  }
   
  // Return the final
  // string hex
  return hex;
}
  
// Driver code
public static void Main(string []arg)
{
  // Print the Hex String
  Console.Write(ASCIItoHEX("Geek"));
}
}
 
// This code is contributed by rutvik_56


Javascript




<script>
      // JavaScript program to convert
      // ASCII string to Hexadecimal
      // format string
      // Function to convert
      // decimal to hexadecimal
      function decToHexa(n) {
        // char array to store
        // hexadecimal number
        var hexaDeciNum = new Array(100).fill(0);
 
        // counter for hexadecimal
        // number array
        var i = 0;
 
        while (n !== 0) {
          // temporary variable
          // to store remainder
          var temp = 0;
 
          // Storing remainder
          // in temp variable.
          temp = n % 16;
 
          // Check if temp < 10
          if (temp < 10) {
            hexaDeciNum[i] = String.fromCharCode(temp + 48);
            i++;
          } else {
            hexaDeciNum[i] = String.fromCharCode(temp + 87);
            i++;
          }
 
          n = parseInt(n / 16);
        }
 
        var ans = "";
 
        // Printing hexadecimal number
        // array in reverse order
        for (var j = i - 1; j >= 0; j--) {
          ans += hexaDeciNum[j];
        }
 
        return ans;
      }
 
      // Function to convert ASCII to HEX
      function ASCIItoHEX(ascii) {
        // Initialize final String
        var hex = "";
 
        // Make a loop to iterate through
        // every character of ascii string
        for (var i = 0; i < ascii.length; i++) {
          // Take a char from
          // position i of string
          var ch = ascii[i];
 
          // Cast char to integer and
          // find its ascii value
          var tmp = ch.charCodeAt(0);
 
          // Change this ascii value
          // integer to hexadecimal value
          var part = decToHexa(tmp);
 
          // Add this hexadecimal value
          // to final string.
          hex += part;
        }
 
        // Return the final
        // string hex
        return hex;
      }
 
      // Driver code
      // Print the Hex String
      document.write(ASCIItoHEX("Geek"));
    </script>


Output

4765656B

Time Complexity: O(n * log16(n)), Where n is the length of the given string.
Auxiliary Space: O(1). 



Last Updated : 08 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads