Skip to content

Instantly share code, notes, and snippets.

@bussiere
Created May 26, 2011 12:49
Show Gist options
  • Save bussiere/993065 to your computer and use it in GitHub Desktop.
Save bussiere/993065 to your computer and use it in GitHub Desktop.
hexa encoder decoder taken from the web
import java.io.UnsupportedEncodingException;
public class Hexa {
public static void main(String[] args) {
// System.out.println(h.encode("Test").toString());
// System.out.println(h.decode("79616c7").toString());
System.out.println(encode("toto"));
System.out.println(encode("★"));
System.out.println(decode("746f746f"));
System.out.println(decode("e29885"));
}
static private final int BASELENGTH = 128;
static private final int LOOKUPLENGTH = 16;
static final private byte [] hexNumberTable = new byte[BASELENGTH];
static final private char [] lookUpHexAlphabet = new char[LOOKUPLENGTH];
static {
for (int i = 0; i < BASELENGTH; i++ ) {
hexNumberTable[i] = -1;
}
for ( int i = '9'; i >= '0'; i--) {
hexNumberTable[i] = (byte) (i-'0');
}
for ( int i = 'F'; i>= 'A'; i--) {
hexNumberTable[i] = (byte) ( i-'A' + 10 );
}
for ( int i = 'f'; i>= 'a'; i--) {
hexNumberTable[i] = (byte) ( i-'a' + 10 );
}
for(int i = 0; i<10; i++ ) {
lookUpHexAlphabet[i] = (char)('0'+i);
}
for(int i = 10; i<=15; i++ ) {
lookUpHexAlphabet[i] = (char)('A'+i -10);
}
}
public static String encode(String input, String charsetName) {
if (input == null) throw new NullPointerException();
try {
return asHex(input.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static String encode(String input) {
String charsetName = "UTF-8";
if (input == null) throw new NullPointerException();
try {
return asHex(input.getBytes(charsetName));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
private static String asHex(byte[] buf)
{
char[] HEX_CHARS = "0123456789abcdef".toCharArray();
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
public static String decode(String hexa)
{
for (int i=0;i < hexa.length();i++)
{
String codehex = hexa.substring(i) + hexa.substring(i++) ;
}
int lengthData = hexa.length();
if (lengthData % 2 != 0)
return null;
char[] binaryData = hexa.toCharArray();
int lengthDecode = lengthData / 2;
byte[] decodedData = new byte[lengthDecode];
byte temp1, temp2;
char tempChar;
for( int i = 0; i<lengthDecode; i++ ){
tempChar = binaryData[i*2];
temp1 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
if (temp1 == -1)
return null;
tempChar = binaryData[i*2+1];
temp2 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1;
if (temp2 == -1)
return null;
decodedData[i] = (byte)((temp1 << 4) | temp2);
}
String retour =new String(decodedData);
return retour;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment