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) {
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.
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.
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.
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.
-Richard
Hey, ruttle, can you post your multibyte code? I'd be interested in seeing it.
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));
Hi,
I just developed a multibyte version of this code.
Here it is:
Regards,
/RB/
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
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.