How to convert hex value to an integer?

689    Asked by PRATEEKArora in Salesforce , Asked on Aug 8, 2021

When 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. How to convert hex to integer?Is there a simple way to do that? Here is my method:

private void GenerateRandomNumber(List lList){ System.debug('*lList*********** '+lList.size()); for(Lead l: lList){ 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); }
Answered by Alan Taylor

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

String hex = '0123456789abcdef'; String[] hexValue = sourceHex.split(''); Decimal result = 0; for(Integer index = 0; index < hexValue xss=removed>

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).

Here are the following are methods for converting hex to Integer value .

format() Returns the integer as a string using the locale of the context user.

valueOf(stringToInteger) Returns an Integer that contains the value of the specified String. ...

valueOf(fieldValue) Converts the specified object to an Integer.

Your Answer