Convert a string to hexadecimal ASCII values
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 :
- Initialize final Hex string as empty.
- Consider every character from input, cast it into integer. This integer value is ascii value of that character.
- Change this integer value into hexadecimal value and add this hexadecimal value to final Hex string.
Basic implementation of the above idea:
C++
#include <iostream>
using namespace std;
string decToHexa( int n)
{
char hexaDeciNum[100];
int i = 0;
while (n != 0) {
int temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
string ans = "" ;
for ( int j = i - 1; j >= 0; j--)
ans += hexaDeciNum[j];
return ans;
}
string ASCIItoHEX(string ascii)
{
string hex = "" ;
for ( int i = 0; i < ascii.length(); i++) {
char ch = ascii[i];
int tmp = ( int )ch;
string part = decToHexa(tmp);
hex += part;
}
return hex;
}
int main()
{
cout << (ASCIItoHEX( "Geek" ));
}
|
Java
import java.util.Scanner;
class ASCIItoHEX {
public static String ASCIItoHEX(String ascii)
{
String hex = "" ;
for ( int i = 0 ; i < ascii.length(); i++) {
char ch = ascii.charAt(i);
int in = ( int )ch;
String part = Integer.toHexString(in);
hex += part;
}
return hex;
}
public static void main(String arg[])
{
System.out.println(ASCIItoHEX( "Geek" ));
}
}
|
Python3
def ASCIItoHEX(ascii):
hexa = ""
for i in range ( len (ascii)):
ch = ascii[i]
in1 = ord (ch)
part = hex (in1).lstrip( "0x" ).rstrip( "L" )
hexa + = part
return hexa
if __name__ = = '__main__' :
print (ASCIItoHEX( "Geek" ))
|
C#
using System;
class GFG{
static string decToHexa( int n)
{
char []hexaDeciNum =
new char [100];
int i = 0;
while (n != 0)
{
int temp = 0;
temp = n % 16;
if (temp < 10)
{
hexaDeciNum[i] =
( char )(temp + 48);
i++;
}
else
{
hexaDeciNum[i] =
( char )(temp + 87);
i++;
}
n = n / 16;
}
string ans = "" ;
for ( int j = i - 1;
j >= 0; j--)
{
ans += hexaDeciNum[j];
}
return ans;
}
public static string ASCIItoHEX( string ascii)
{
string hex = "" ;
for ( int i = 0;
i < ascii.Length; i++)
{
char ch = ascii[i];
int tmp = ( int )ch;
string part = decToHexa(tmp);
hex += part;
}
return hex;
}
public static void Main( string []arg)
{
Console.Write(ASCIItoHEX( "Geek" ));
}
}
|
Javascript
<script>
function decToHexa(n) {
var hexaDeciNum = new Array(100).fill(0);
var i = 0;
while (n !== 0) {
var temp = 0;
temp = n % 16;
if (temp < 10) {
hexaDeciNum[i] = String.fromCharCode(temp + 48);
i++;
} else {
hexaDeciNum[i] = String.fromCharCode(temp + 87);
i++;
}
n = parseInt(n / 16);
}
var ans = "" ;
for ( var j = i - 1; j >= 0; j--) {
ans += hexaDeciNum[j];
}
return ans;
}
function ASCIItoHEX(ascii) {
var hex = "" ;
for ( var i = 0; i < ascii.length; i++) {
var ch = ascii[i];
var tmp = ch.charCodeAt(0);
var part = decToHexa(tmp);
hex += part;
}
return hex;
}
document.write(ASCIItoHEX( "Geek" ));
</script>
|
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
Share your thoughts in the comments
Please Login to comment...