Konwertuj znak na wartość liczbową ASCII w Javie
Mam String name = "admin";
then I do String char = name.substring(0,1); //char="a"
Chcę przekonwertować char
na jego wartość ASCII (97), jak to zrobić w Javie?
19 answers
Bardzo proste. Wystarczy rzucić {[3] } jako int
.
char character = 'a';
int ascii = (int) character;
W Twoim przypadku musisz najpierw pobrać konkretny znak z ciągu znaków, a następnie go rzucić.
char character = name.charAt(0); // This gives the character 'a'
int ascii = (int) character; // ascii is now 97.
Chociaż obsada nie jest wymagana wyraźnie, ale poprawia czytelność.
int ascii = character; // Even this will do the trick.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:44:17
Just a different approach
String s = "admin";
byte[] bytes = s.getBytes("US-ASCII");
bytes[0]
będzie reprezentować ascii z a.. i tym samym pozostałe znaki w całej tablicy.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:37:37
Zamiast tego:
String char = name.substring(0,1); //char="a"
Należy użyć metody charAt()
.
char c = name.charAt(0); // c='a'
int ascii = (int)c;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:32:13
Jeśli chcesz przekonwertować cały łańcuch znaków na skonkatenowane wartości ASCII, możesz użyć tego-
String str = "abc"; // or anything else
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c);
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);
W którym otrzymasz 979899 jako wyjście.
Kredyt na to .
Skopiowałem go tutaj, aby był wygodny dla innych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:18:22
Kilka odpowiedzi, które mają pokazać, jak to zrobić, jest błędnych, ponieważ znaki Javy nie są znakami ASCII. Java używa wielobajtowego kodowania znaków Unicode. Zestaw znaków Unicode jest super zestawem ASCII. Tak więc w łańcuchu Java mogą znajdować się znaki, które nie należą do ASCII. Takie znaki nie mają wartości liczbowej ASCII, więc pytanie, Jak uzyskać wartość liczbową ASCII znaku Java, jest bez odpowiedzi.
Ale Dlaczego w ogóle chcesz to zrobić? Co zrobisz z tą wartością?
Jeśli chcesz mieć wartość liczbową, aby można było przekonwertować Łańcuch Java na łańcuch ASCII, pytanie Prawdziwe brzmi "jak zakodować Łańcuch Java jako ASCII". W tym celu użyj obiektu StandardCharsets. US_ASCII.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-23 10:22:39
Konwertuj znak na int.
String name = "admin";
int ascii = name.toCharArray()[0];
Także:
int ascii = name.charAt(0);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:33:25
Po prostu wrzuć znak do int.
char character = 'a';
int number = (int) character;
Wartość number
będzie 97.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:31:52
To proste, pobierz znak, który chcesz, i przekonwertować go do int.
String name = "admin";
int ascii = name.charAt(0);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 09:42:49
Wiem, że na to już odpowiedziano w kilku formach, ale oto mój fragment kodu z wyglądem, aby przejść przez wszystkie znaki.
Oto kod zaczynający się od klasy
public class CheckChValue { // Class name
public static void main(String[] args) { // class main
String name = "admin"; // String to check it's value
int nameLenght = name.length(); // length of the string used for the loop
for(int i = 0; i < nameLenght ; i++){ // while counting characters if less than the length add one
char character = name.charAt(i); // start on the first character
int ascii = (int) character; //convert the first character
System.out.println(character+" = "+ ascii); // print the character and it's value in ascii
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-07 15:43:59
String str = "abc"; // or anything else
// Stores strings of integer representations in sequence
StringBuilder sb = new StringBuilder();
for (char c : str.toCharArray())
sb.append((int)c);
// store ascii integer string array in large integer
BigInteger mInt = new BigInteger(sb.toString());
System.out.println(mInt);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-22 14:03:15
String name = "admin";
char[] ch = name.toString().toCharArray(); //it will read and store each character of String and store into char[].
for(int i=0; i<ch.length; i++)
{
System.out.println(ch[i]+
"-->"+
(int)ch[i]); //this will print both character and its value
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-07 06:38:17
public class Ascii {
public static void main(String [] args){
String a=args[0];
char [] z=a.toCharArray();
for(int i=0;i<z.length;i++){
System.out.println((int)z[i]);
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-09 15:23:10
Jak zauważył @Raedwald, Unicode Javy nie obsługuje wszystkich znaków, aby uzyskać wartość ASCII. Poprawny sposób (Java 1.7+) jest następujący:
byte[] asciiBytes = "MyAscii".getBytes(StandardCharsets.US_ASCII);
String asciiString = new String(asciiBytes);
//asciiString = Arrays.toString(asciiBytes)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-19 11:12:26
Łatwym sposobem na to jest:
int character = 'a';
Jeśli wydrukujesz "znak", otrzymasz 97.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-22 03:58:14
Możesz też użyć Stream API dla 1 znaku lub ciągu znaków zaczynającego się w Javie 1.8:
public class ASCIIConversion {
public static void main(String[] args) {
String text = "adskjfhqewrilfgherqifvehwqfjklsdbnf";
text.chars()
.forEach(System.out::println);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-12 07:47:45
Możesz sprawdzić numer Ascis za pomocą tego kodu.
String name = "admin";
char a1 = a.charAt(0);
int a2 = a1;
System.out.println("The number is : "+a2); // the value is 97
Jeśli się mylę, przepraszam.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-23 10:18:48
Jeśli chcesz mieć wartość ASCII wszystkich znaków w łańcuchu. Możesz użyć tego:
String a ="asdasd";
int count =0;
for(int i : a.toCharArray())
count+=i;
A jeśli chcesz ASCII pojedynczego znaku w łańcuchu możesz wybrać:
(int)a.charAt(index);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-11 18:54:04
Próbowałem tego samego, ale najlepszym i łatwym rozwiązaniem byłoby użycie charAt i aby uzyskać dostęp do indeksów, powinniśmy utworzyć tablicę liczb całkowitych o rozmiarze [128].
String name = "admin";
int ascii = name.charAt(0);
int[] letters = new int[128]; //this will allocate space with 128byte size.
letters[ascii]++; //increments the value of 97 to 1;
System.out.println("Output:" + ascii); //Outputs 97
System.out.println("Output:" + letters[ascii]); //Outputs 1 if you debug you'll see 97th index value will be 1.
Jeśli chcesz wyświetlić wartości ASCII całego ciągu znaków, musisz to zrobić.
String name = "admin";
char[] val = name.toCharArray();
for(char b: val) {
int c = b;
System.out.println("Ascii value of " + b + " is: " + c);
}
Twój wynik, w tym przypadku, będzie: Wartość Ascii a wynosi: 97 Ascii wartość d wynosi: 100 Wartość Ascii m wynosi: 109 Wartość Ascii i wynosi: 105 Wartość Ascii n wynosi: 110
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-30 20:44:26
Najprostszym sposobem na to jest:
Dla całego ciągu znaków w ASCII:
public class ConvertToAscii{
public static void main(String args[]){
String abc = "admin";
int []arr = new int[abc.length()];
System.out.println("THe asscii value of each character is: ");
for(int i=0;i<arr.length;i++){
arr[i] = abc.charAt(i); // assign the integer value of character i.e ascii
System.out.print(" "+arr[i]);
}
}
}
wyjście to:
THe asscii value of each character is:
97 100 109 105 110
Tutaj
abc.charAt(i)
podaje pojedynczy znak tablicy łańcuchowej:
Gdy przypisujemy każdy znak do typu integer wtedy kompilator wykonuje konwersję typu jako,
arr[i] = (int) character // Here, every individual character is coverted in ascii value
Ale dla pojedynczego znaku:
String name = admin;
asciiValue = (int) name.charAt(0);// for character 'a'
System.out.println(asciiValue);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-31 15:38:24