I'm currently trying to write an encryption program and I'm trying to figure out how a user can enter a string (a mix of number, letters, and special characters) and have the program convert each character to its numeric ASCII value. I've searched all over the internet and I can't seem to find any method or line of code that can do this. Now before anyone says "Try casting it to an integer" I've tried and it doesn't work. So if anyone could please help me in this matter I would be very appreciative.
Asked
Active
Viewed 1,627 times
1
-
What do you mean by "it doesn't work"? – Klitos Kyriacou Oct 14 '18 at 12:18
-
@Kiltos_Kyriscou I mean it causes an error which says incompatible types: java.lang.String cannot be converted to int. – Gh0stwarr10r Oct 14 '18 at 12:25
-
A string is not a character, but a list of characters. You have to pick a character to pull out of the string. – Daniel Pryden Oct 14 '18 at 12:28
-
1More fundamentally: a string is a list of *characters* (technically, a list of Unicode code points that have been encoded into UTF-16 code units). But encryption algorithms typically work at the level of bytes instead. So rather than assuming that characters and bytes are the same thing (which they aren't, even though C confusingly uses `char` to refer to a byte), you need to convert data into bytes before doing byte-oriented operations on it. – Daniel Pryden Oct 14 '18 at 12:33
2 Answers
3
To turn characters into numbers, you need pick an encoding. US-ASCII is one way to do that, but note, US-ASCII only defines about 95 characters; any character that isn't one of those 95 causes the process of converting to bytes to throw an exception. UTF-8 is the usual go-to choice when you want to be able to convert all characters (from ü to ☃ to emojis). Note in UTF-8, any non-ASCII character ends up being multiple bytes for one character, so a 10-char string might end up being as many as 50 bytes.
String test = "Hello!";
byte[] asBytes = test.getBytes(StandardCharsets.US_ASCII);
String fancy = "Wééé! ☃";
byte[] fancyBytes = fancy.getBytes(StandardCharsets.UTF_8);

rzwitserloot
- 85,357
- 5
- 51
- 72
-
1Just to clarify, US-ASCII defines 128 characters, of which 95 are printable. – Klitos Kyriacou Oct 14 '18 at 12:17
-
@rzwitserloot I thought strings in Java were encoded in UTF-16 and couldn't be modified or encoded to UTF-8 – Gh0stwarr10r Oct 14 '18 at 12:42
-
@Gh0stwarr10r No, forget about that: Internally, java stores strings in a UTF-16-esque format, but .getBytes converts. It doesn't change what the string's internal representation is (it doesn't change the string at all; strings are immutable), it makes a new byte array. – rzwitserloot Oct 14 '18 at 15:14
-
1
You can loop through the string and use the charAt() method of the string:
String s = "abcd";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
int value = s.charAt(i);
System.out.println(c + " " + value);
}
will print:
a 97
b 98
c 99
d 100

forpas
- 160,666
- 10
- 38
- 76