6

When I do the code below, I get a much larger number (not HEX). Am I missing some type of additional conversion that needs to be done?

The code below generates the value: M34363533 when I'm expecting M122D (please note I add on the "M" in my code).

public void GenerateUniqueCode(Custom_Object__c scope) {

        string target = scope.Name;
        integer n = Integer.valueOf(target) + 4096; //need at least 5 characters
       //convert back to string so that I can send to blob
        String snum = String.valueOf(n);
        Blob b = blob.valueof(snum); 

        String result = EncodingUtil.convertToHex(b);

//code cannot have "B" in it, so replacing it
        result = result.replace('B','X');

//adding an initial identifier to the code
        scope.unique_Code__c = 'M' + result;

    }

2 Answers 2

5

I think the method you are using is converting each byte into hex rather than the overall number.

AFAIK there is not the equivalent of e.g. Java's Integer.toHexString method available in Apex. But the source code of such methods is available that you can port to Apex. A basic port would be:

public class Integers {

    private static final String [] digits = new String[] {
           '0' , '1' , '2' , '3' , '4' , '5' ,
           '6' , '7' , '8' , '9' , 'a' , 'b' ,
           'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
           'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
           'o' , 'p' , 'q' , 'r' , 's' , 't' ,
           'u' , 'v' , 'w' , 'x' , 'y' , 'z'
           };
    private static final Integer shift = 4;

    public static String toHexString(Integer i) {

        String[] buf = new String[32];
        Integer charPos = 32;
        Integer radix = 1 << shift;
        Integer mask = radix - 1;
        do {
           buf[--charPos] = digits[i & mask];
           i >>>= shift;
        } while (i != 0);

        String result = '';
        for (Integer pos = charPos; pos < 32; pos++) result += buf[pos];
        return result;
    }
}

but you should also write a unit test to confirm it is working and consider optimising it further (as Apex doesn't have the same set of capabilities) if you are going to call it often.

(The coding approach here of using bitwise shifts and bitwise masks to do the divide by 16 and remainder 16 logic is the sort of optimization that really belongs in well-tested libraries not application code.)

2

Apex code to convert a Long To Hexa decimal string.

    Class LongToHex{
        public static string HexCalculate(long num){
            integer rem;
            String str2=''; 
            String[] hex=new String[]{'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
                while(num>0)
            {
                rem=(integer)Math.mod(num, 16); 
                str2=hex[rem]+str2; 
                num=num/16;
            }
            return str2;
        }

        public static String TimeConversion(){
            String str = LongToHex.HexCalculate(/* PASS UR  VALUE*/);
            System.debug('final string is     '+str);
            return str;
        }
    }
2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .