I need to turn string into an array of integers. Each array slot would contain one letter in integer value. For example, string omg would be returned as something like
array[0] = 13; array[1] = 44; array[3] = 26;
EDIT These values of letters are thought up. I just want to know if there is a method to turn letters to integers.
Is there any builtin method for this?
There are a varieties of ways of doing this, depending on exactly what you mean by “turn letters to integers”.
You can use
String.getBytes()to encode the string into the system’s default encoding and give you that as abyte[]. Thebytetype is an integer type.You can use
String.toCharArray()to give you the string’s characters. Thechartype is an integer type.You could do various other “mystery” transformations on the string; e.g. apply Caeser’s cipher to each string character to give you a letter that you then turn into a number.
And you could then write a loop to turn a
char[]orbyte[]into anint[], but there is no built-in method to do this step of the transformation … if that’s what you need.Note that the first two approaches may give you different integers, depending on the string value and the system’s default character encoding. It is important that you understand what you are trying to do with this conversion …
(I’m assuming that the example transformation in the Question should not be taken literally …)