 ShowAll Questionssorted byDate Posted Chamiz

# Method to convert Hex to String or blob

Apex provides supports for convert String to Hex value but not the Hex value to String. Can you help me on this.

Thank you rtuttle

I use the following code for converting hex strings into a list of integers. Once you have the list of integer values (the character code for each letter) you can then convert to string using String.fromCharArray( List<Integer> )

This gets a bit trickier if you're using multibyte language encoding.  Let me know if you are and I'll post some code that converts from multibyte hex to string.

Also, try to cut some slack to SFDC.  This is a growing platform and you should expect some growing pains.  The ideaexchange is a great place for you to help improve the platform.  If you find something like this is a pain, then perhaps you could submit an idea and post your idea in your post.  I've been there before, I had to write an entire class for converting data for encryption before they launched encryption in Summer '10.

private static Map<String,Integer> hexMap = new Map<String,Integer>();
static {
hexMap.put('0',0);
hexMap.put('1',1);
hexMap.put('2',2);
hexMap.put('3',3);
hexMap.put('4',4);
hexMap.put('5',5);
hexMap.put('6',6);
hexMap.put('7',7);
hexMap.put('8',8);
hexMap.put('9',9);
hexMap.put('A',10);
hexMap.put('B',11);
hexMap.put('C',12);
hexMap.put('D',13);
hexMap.put('E',14);
hexMap.put('F',15);
hexMap.put('a',10);
hexMap.put('b',11);
hexMap.put('c',12);
hexMap.put('d',13);
hexMap.put('e',14);
hexMap.put('f',15);
}
public static List<Integer> stringToInt(String input) {
String hex = EncodingUtil.convertToHex(Blob.valueOf(input));
List<Integer> retValues = new List<Integer>();
for(Integer i=0;i<hex.length();i+=2) {
}
return
```	private static Map<String,Integer> hexMap = new Map<String,Integer>();
static {
hexMap.put('0',0);
hexMap.put('1',1);
hexMap.put('2',2);
hexMap.put('3',3);
hexMap.put('4',4);
hexMap.put('5',5);
hexMap.put('6',6);
hexMap.put('7',7);
hexMap.put('8',8);
hexMap.put('9',9);
hexMap.put('A',10);
hexMap.put('B',11);
hexMap.put('C',12);
hexMap.put('D',13);
hexMap.put('E',14);
hexMap.put('F',15);
hexMap.put('a',10);
hexMap.put('b',11);
hexMap.put('c',12);
hexMap.put('d',13);
hexMap.put('e',14);
hexMap.put('f',15);
}

public static List<Integer> hexToInt(String hex) {
List<Integer> retVal = new List<Integer>();
for(Integer i=0;i<hex.length();i+=2) {
retVal.add( (hexMap.get(hex.substring(i,i+1)) * 16) + (hexMap.get(hex.substring(i+1,i+2))) );
}
return retVal;
```

```private static Map<String,Integer> hexMap = new Map<String,Integer>();
static {
hexMap.put('0',0);
hexMap.put('1',1);
hexMap.put('2',2);
hexMap.put('3',3);
hexMap.put('4',4);
hexMap.put('5',5);
hexMap.put('6',6);
hexMap.put('7',7);
hexMap.put('8',8);
hexMap.put('9',9);
hexMap.put('A',10);
hexMap.put('B',11);
hexMap.put('C',12);
hexMap.put('D',13);
hexMap.put('E',14);
hexMap.put('F',15);
hexMap.put('a',10);
hexMap.put('b',11);
hexMap.put('c',12);
hexMap.put('d',13);
hexMap.put('e',14);
hexMap.put('f',15);
}

public static List<Integer> hexToInt(String hex) {
List<Integer> retVal = new List<Integer>();
for(Integer i=0;i<hex.length();i+=2) {
retVal.add( (hexMap.get(hex.substring(i,i+1)) * 16) + (hexMap.get(hex.substring(i+1,i+2))) );
}
return retVal;
}```

-Richard thecrmninja

Hey, ruttle, can you post your multibyte code?  I'd be interested in seeing it. deepakpi

Hi Richard,

I ran below code on System log window. But I still do'nt get same hex string back.

Am I doing something wrong?

Map<String,Integer> hexMap = new Map<String,Integer>();
hexMap.put('0',0);
hexMap.put('1',1);
hexMap.put('2',2);
hexMap.put('3',3);
hexMap.put('4',4);
hexMap.put('5',5);
hexMap.put('6',6);
hexMap.put('7',7);
hexMap.put('8',8);
hexMap.put('9',9);
hexMap.put('A',10);
hexMap.put('B',11);
hexMap.put('C',12);
hexMap.put('D',13);
hexMap.put('E',14);
hexMap.put('F',15);
hexMap.put('a',10);
hexMap.put('b',11);
hexMap.put('c',12);
hexMap.put('d',13);
hexMap.put('e',14);
hexMap.put('f',15);
String hexString='CE993A13754340D1FED14A9EFB4A058D';
System.debug('hexString=' + hexString);
List<Integer> retVal = new List<Integer>();
for(Integer i=0;i<hexString.length();i+=2) {
retVal.add( (hexMap.get(hexString.substring(i,i+1)) * 16) + (hexMap.get(hexString.substring(i+1,i+2))) );
}
System.debug('retVal=' + retVal);
String str=String.fromCharArray(retVal);
System.debug('str=' + str);
Blob b=Blob.valueOf(str);
System.debug(' hexString=' + EncodingUtil.convertToHex(b)); rui_barata

Hi,

I just developed a multibyte version of this code.

Here it is:

```// convert Hex to UTF-8 (now works with multibyte locales!)
//http://boards.developerforce.com/t5/Apex-Code-Development/Method-to-convert-Hex-to-String-or-blob/td-p/187747#M30705
private static Map<String,Integer> hexMap = new Map<String,Integer>();
static {
hexMap.put('0',0);
hexMap.put('1',1);
hexMap.put('2',2);
hexMap.put('3',3);
hexMap.put('4',4);
hexMap.put('5',5);
hexMap.put('6',6);
hexMap.put('7',7);
hexMap.put('8',8);
hexMap.put('9',9);
hexMap.put('A',10);
hexMap.put('B',11);
hexMap.put('C',12);
hexMap.put('D',13);
hexMap.put('E',14);
hexMap.put('F',15);
hexMap.put('a',10);
hexMap.put('b',11);
hexMap.put('c',12);
hexMap.put('d',13);
hexMap.put('e',14);
hexMap.put('f',15);
}

public class UTFException extends Exception {}

public static List<Integer> hexToInt(String hex) {
List<Integer> retVal = new List<Integer>();
Integer i=0;
while(i<hex.length()) {

// http://en.wikipedia.org/wiki/UTF-8
integer numberOfBytes=1;
integer byte1=0, byte2=0, byte3=0, byte4=0;
integer utfCode=0;
byte1=(hexMap.get(hex.substring(i,i+1)) * 16) + (hexMap.get(hex.substring(i+1,i+2))) ;

//invalid sequences for byte1
if(byte1>=128 && byte1<=191) {
throw new UTFException('UTF-8:Continuation byte as first byte');
}
if(byte1>=192 && byte1<=193) {
throw new UTFException('UTF-8:Invalid 2-byte sequence');
}
if(byte1>=245) {
throw new UTFException('UTF-8:Invalid 4,5 or 6-byte sequence');
}

if(byte1>=192) {
numberOfBytes=2;
byte2=(hexMap.get(hex.substring(i+2,i+2+1)) * 16) + (hexMap.get(hex.substring(i+2+1,i+2+2))) ;
}
if(byte1>=224) {
numberOfBytes=3;
byte3=(hexMap.get(hex.substring(i+4,i+4+1)) * 16) + (hexMap.get(hex.substring(i+4+1,i+4+2))) ;
}
if(byte1>=240) {
numberOfBytes=4;
byte4=(hexMap.get(hex.substring(i+6,i+6+1)) * 16) + (hexMap.get(hex.substring(i+6+1,i+6+2))) ;
}
if(numberOfBytes==1) {
utfCode=byte1;
} else if(numberOfBytes==2) {
utfCode=Math.mod(byte1,32)*64+Math.mod(byte2,64);
} else if(numberOfBytes==3) {
utfCode=Math.mod(byte1,16)*64*64+Math.mod(byte2,64)*64+Math.mod(byte3,64);
} else if(numberOfBytes==4) {
utfCode=Math.mod(byte1,8)*64*64*64+Math.mod(byte2,64)*64*64+Math.mod(byte3,64)*64+Math.mod(byte4,64);
}

i+=2*numberOfBytes;
}
return retVal;
}

public static Blob HexToUTF(string hex) {
string text=String.fromCharArray(hexToInt(hex));
return Blob.valueOf(text);
}```

Regards,

/RB/ rtuttle

Hi RB,

Nice job.  I actually had already converted it to multibyte.  I should have posted it here, my apologies.  Glad you figured out the multibyte conversion though.

-Richard rui_barata
Well, thank you. No problem, I actually enjoyed coding it. Best regards /RB/ shrutiiiii

I am working on WebServices and want to display the image I am getting in WebService response on Visualforce.  I am trying to handle WebService response in Hex (i.e. EncodingUtil.convertToHex(HttpResponse.getBodyAsBlob()) which is multipart/related response and includes image and Html part of image. I could not handle it as String (i.e. HttpResponse.getBody()) as, I found some data loss of image.

I got the image in Hex and want to convert it to Base64 so that, I could display it on Visualforce as Base64 embedded image in HTML. I want to convert Hex to Blob and Blob to Base64. <please see this http://boards.developerforce.com/t5/Apex-Code-Development/Conversion-from-Hex-to-Blob-in-apex-class/td-p/547321>

But, I am not finding any way for this conversion and function listed is not helping me as it is handling UTF-8 strings only. Can anyone please help me on this.