如何在java中将字符串转换为ascii值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26724576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to cast string to ascii value in java?
提问by Hamed Fazilat
I have new assignment and since I am new to JAVA I don't know how to make it work, I have searched this website frequently but all the solutions which were suggested didn't work out for me or I didn't use them properly, I would be grateful if someone helps me... the code below is the most simple solution I could find but still doesn't work...
我有新的任务,因为我是 JAVA 的新手,我不知道如何让它工作,我经常搜索这个网站,但所有建议的解决方案都不适合我,或者我没有正确使用它们,如果有人帮助我,我将不胜感激......下面的代码是我能找到的最简单的解决方案,但仍然不起作用......
I want to get inputs like names from people and change them to numbers (int)...it says it is not possible to cast from string to int ... !!
我想从人们那里获得诸如姓名之类的输入并将它们更改为数字(int)......它说不可能从字符串转换为 int ... !!
package loveindex;
import java.util.Scanner;
//import java.math.BigInteger;
public class LoveIndex {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
System.out.println("Testing Scanner, write something: ");
String testi = scan.nextLine();
System.out.println(testi);
System.out.println("Testing Scanner, write something: ");
String testi2 = scan.nextLine();
System.out.println(testi2);
int ascii = (int) testi;
int ascii = (int) testi2;
}
}
采纳答案by AsSiDe
You Can Try This:
你可以试试这个:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Testing Scanner, write something: ");
String testi = scan.nextLine();
char[] ascii1 = testi.toCharArray();
for(char ch:ascii1){
System.out.println((int)ch+" ");
}
System.out.println("Testing Scanner, write something: ");
String testi2 = scan.nextLine();
char[] ascii2 = testi2.toCharArray();
for(char ch:ascii2){
System.out.println((int)ch+" ");
}
scan.close();
}
回答by Reimeus
You're attempting to assign a String
to an int
which the 2 of which are incompatible. You can get the ascii value of a single character
您正在尝试将 a 分配String
给int
其中 2 个不兼容的an 。可以获取单个字符的ascii值
int ascii = testi.charAt(0);
回答by Johny
You cannot convert string to ascii like that. You can convert a character to ascii
您不能像那样将字符串转换为 ascii。您可以将字符转换为 ascii
int ascii = (int)some_char;
回答by Tetramputechture
First of all: Java doesn't use ASCII. It use Unicode (UTF-16, most of the time).
首先:Java 不使用 ASCII。它使用 Unicode(大多数情况下是 UTF-16)。
Second: You can make a function to convert your String characters to their Unicode representations, like this:
第二:您可以创建一个函数将您的字符串字符转换为它们的 Unicode 表示,如下所示:
public static int[] convert(String str) {
int[] result = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
result[i] = str.charAt(i);
}
return result;
}
回答by Bhanu Hoysala
Achieve the same in a concise way by employing Java 8's lambda function. From your comment (at the accepted answer) you need the sum of the characters at the end?
通过使用 Java 8 的 lambda 函数以简洁的方式实现相同的目标。根据您的评论(在接受的答案中),您需要最后的字符总和?
String str = "name";
int sum = str.chars().reduce(0, Integer::sum);