function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrey BolkonskyAndrey Bolkonsky 

converting a List of ASCII codes into the actual Integers

Another newbie quetion. I thought I was creating a List that took the value of my Integer variable "Amount" and put each digit as a value of the array. Instead I got the ASCII values! Not helpful at all.
 
public String writeNumber() {
            String amountString=String.valueOf(Amount);
            Len=amountString.Length();
                
            List<Integer> arrayNumber=new List<Integer>();
                for(Integer i=0; i <= Len - 1; i++) {
                arrayNumber.add(amountString.charAt(i));
                }
           }
If i put in 30567 for the value of Amount, I thought that {3, 0, 5, 6, 7} would be the values of my list but instead it is {51, 48, 53, 54, 55}. How can I get this to have the values that I want and not ASCII?
Best Answer chosen by Andrey Bolkonsky
Malika Pathak 9Malika Pathak 9
Hi Andrey,

you can try this:
Integer amount = 12345;
String amountString = String.Valueof(amount);
List<Integer> integerList = new List<Integer>();

String[] chars = amountString.split('');
    for(String c : chars){
       if(c.isNumeric()){
        integerList.add(Integer.valueOf(c));
      }
}
System.debug('<<integerList>> '+integerList);

If you find this helpful, mark this as the best answer to help others.

All Answers

Malika Pathak 9Malika Pathak 9
Hi Andrey,

you can try this:
Integer amount = 12345;
String amountString = String.Valueof(amount);
List<Integer> integerList = new List<Integer>();

String[] chars = amountString.split('');
    for(String c : chars){
       if(c.isNumeric()){
        integerList.add(Integer.valueOf(c));
      }
}
System.debug('<<integerList>> '+integerList);

If you find this helpful, mark this as the best answer to help others.
This was selected as the best answer
Andrey BolkonskyAndrey Bolkonsky

Thanks Malika! Is there a name for this type of for loop notation style so I can learn more about it? I am only used to seeing them written like:

for( initialization; condition ; incrementation ) {};