How to turn each character of a string into its ASCII character code and join them together in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "java programming";
        String ascii = "";
       
        for (int i = 0; i < s.length(); i++) {
            int asciicode = (byte)s.charAt(i);
            System.out.printf("%d ", asciicode);
            ascii = ascii + asciicode;
            
        }
        System.out.println("\n" + ascii);
    }
}
 
 
 
/*
run:
 
106 97 118 97 32 112 114 111 103 114 97 109 109 105 110 103 
10697118973211211411110311497109109105110103
 
*/

 



answered Apr 2, 2021 by avibootz
...