convert hex value to integer

I am working on a method defined in a class that controls the lead object. The desired end result is to produce a unique number for each Lead record. I currently have this working, but the out put is a hex value and I need it to be integer. The field to populate is called "Unique_Reference_number__C". An auto-number field is not sufficient, as there are different formats applied contingent on lead field values. Is there a simple way to convert a hex value to an integer? Here is my method:

private void GenerateRandomNumber(List<Lead> lList){
System.debug('*lList*********** '+lList.size());
if(l.Unique_Reference_number__c==null || l.Unique_Reference_number__c==''){
System.debug('*1*********** ');
Integer len = 7;
Blob blobKey = crypto.generateAesKey(128);
String key = EncodingUtil.(blobKey);
String pwd = 'ID'+key.substring(0,len);
l.Unique_Reference_number__c= pwd;
System.debug('*1*********** '+pwd);
}

You can convert hex to a value using a few primitive functions:

String hex = '0123456789abcdef';
String[] hexValue = sourceHex.split('');
Decimal result = 0;
for(Integer index = 0; index < hexValue.size(); index++) {
result = (result * 16) + hex.indexOf(hexValue[index]);
}
return result;

However, you're likely going to lose precision with a 128-bit key, so you might consider using simply a 64-bit key instead (and put it in a Long instead of Decimal).

• Thanks, this logic worked great! I appreciate your help @sfdcfox! Commented Jun 20, 2016 at 15:31
• Great solution. One important note thought you want to first convert the sourceHex to lower case like so sourceHex.toLowercase().split(''); Commented Jun 27, 2021 at 21:25

hex to int conversion:

public static final List<String> hex = '0123456789abcdef'.split('');
public static Integer hexToInt(String str) {
str = str.toLowerCase();
String[] hexValue = str.split('');
Integer retVal = 0;
for(Integer i = 0; i < hexValue.size(); i++) {
retVal = (retVal << 4) + hex.indexOf(hexValue[i]);
}
return retVal;
}

And for anyone interested the int to hex:

public static final List<String> hex = '0123456789abcdef'.split('');//same as above
public static string intToHex (Integer num){
integer rem;
String retVal='';
while(num>0)
{
rem = (integer) Math.mod(num, 16);
retVal = hex[rem] + retVal;
num = num / 16;
}
return retVal.leftPad(1, '0');//change 1 to 2 if you are converting from hex to strings afterwards
}

Please refer below solution for your problem.

Hex to String Conversion

• There's no BigInteger in apex code?
– sfdcfox
Commented Jun 16, 2016 at 20:57

Here is a function that worked for me:

Decimal hexToDecimal(String hex) {
String[] hexCharacters = hex.toLowerCase().split('');
Decimal result = 0;
for (Integer i = 0; i < hexCharacters.size(); i++) {
switch on hexCharacters[i] {
when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' {
result = (result * 16) + Integer.valueOf(hexCharacters[i]);
}
when 'a' {
result = (result * 16) + 10;
}
when 'b' {
result = (result * 16) + 11;
}
when 'c' {
result = (result * 16) + 12;
}
when 'd' {
result = (result * 16) + 13;
}
when 'e' {
result = (result * 16) + 14;
}
when 'f' {
result = (result * 16) + 15;
}
when else {

}
}
}
return result;
}

The test code yields the expected results: