1

I am trying to make a method that will take each character from a string, convert each charcter to ascii and then populate an array with the ascii values.

The method I have made prints random characters so I must have an issue in this funtion.

public class Caesar {

public int[] stringToSymbolArray(String str){
    str = str.toUpperCase();
    int stringLength = str.length();

    int[] symbolArray = new int[str.length()];

    for (int i = 0; i < stringLength; i++) {
        char character = str.charAt(i);
        int ascii = (int) character;
        symbolArray[i] = ascii;
    }
    return symbolArray;
}

}

Bulldog
  • 11
  • 1
  • What do you mean by random characters? Do you have Unicode characters that aren't ASCII in that string? Also, I think you can just use `getBytes` instead of all of this – user May 07 '20 at 14:07
  • 1
    This might answer your question: https://stackoverflow.com/questions/5688042/how-to-convert-a-java-string-to-an-ascii-byte-array – user May 07 '20 at 14:09
  • "prints random characters" - what do you mean by that? Maybe you just have a problem with printing an array and values are correct? – Amongalen May 07 '20 at 14:11
  • By random characters I mean the output I get is [I@5594a1b5. I am new to programming and this output confuses me. – Bulldog May 07 '20 at 14:12
  • @AndyC what ascii characters are you wanting? hex values, decimal values, oct values? for example, the letter 'H' is 0x48 in hex but 72 in decimal – james May 07 '20 at 14:15
  • 1
    @Amongalen I was incorrectly printing my array. The post you linked helped me fix this. Using System.out.println(Arrays.toString(array)); printed the expected output thank you. – Bulldog May 07 '20 at 14:16

0 Answers0