Converting a string to hex and vice-versa in perl, javascript, and java

Published on Author p3lo1 Comment

Conversion of a string to hex and vice-versa can be used as a simple encryption method when passing values or parameters between two or more applications or scripts. In this article we will show how to convert a string to hex and then convert it back in Perl, Java, JavaScript.

 

Perl – Conversion String to Hex

#!/usr/bin/perl
use strict;
use warnings;

my $hex_value =  unpack("H*","www.code-monk.net");
print "Hex value: $hex_value\n";

Output

Hex value: 7777772e636f64652d6d6f6e6b2e6e6574

 

Perl – Conversion Hex to String

#!/usr/bin/perl
use strict;
use warnings;

my $ascii_value =  pack("H*","7777772e636f64652d6d6f6e6b2e6e6574");
print "Ascii value: $ascii_value\n";

Output

Ascii value: www.code-monk.net

 

JavaScript – Conversion String to Hex

function a2hex(str) {
  var arr = [];
  for (var i = 0, l = str.length; i < l; i ++) { 
      var hex = Number(str.charCodeAt(i)).toString(16); 
      arr.push(hex.length > 1 && hex || "0" + hex);
  }
  return arr.join('');
}
a2hex('www.code-monk.net');

Output

7777772e636f64652d6d6f6e6b2e6e6574

 

JavaScript – Conversion Hex to String

function hex2a(hexx) {
    var hex = hexx.toString();
    var str = '';
    for (var i = 0; i < hex.length; i += 2)
        str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
    return str;
}
hex2a('7777772e636f64652d6d6f6e6b2e6e6574');

Output

www.code-monk.net

 

Java – Conversion String to Hex

package AsciitoHex;

public class AsciitoHex {
	private static String asciiToHex(String asciiValue)
	   {
	      char[] chars = asciiValue.toCharArray();
	      StringBuffer hex = new StringBuffer();
	      for (int i = 0; i < chars.length; i++)
	      {
	         hex.append(Integer.toHexString((int) chars[i]));
	      }
	      return hex.toString();
	   }

	public static void main(String[] args) {
		String ascii_value = "www.code-monk.net";
		String hex_value = asciiToHex(ascii_value);
		System.out.println("Hex String: "+ hex_value);
	}
}

Output

Hex String:7777772e636f64652d6d6f6e6b2e6e6574

 

Java – Conversion Hex to String

package HextoAscii;

public class HextoAscii {
	private static String hexToASCII(String hexValue)
	   {
	      StringBuilder output = new StringBuilder("");
	      for (int i = 0; i < hexValue.length(); i += 2)
	      {
	         String str = hexValue.substring(i, i + 2);
	         output.append((char) Integer.parseInt(str, 16));
	      }
	      return output.toString();
	   }

	public static void main(String[] args) {
		String hex_value = "7777772e636f64652d6d6f6e6b2e6e6574";
		String ascii_value = hexToASCII(hex_value);
		System.out.println("Ascii String: "+ ascii_value);
	}
}

Output

Ascii String: www.code-monk.net

 

Share on FacebookShare on Google+Tweet about this on TwitterEmail this to someoneShare on RedditShare on LinkedIn

One Response to

Converting a string to hex and vice-versa in perl, javascript, and java

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.