java 如何在Android中将ASCII字符转换为十进制数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17910117/
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 convert ASCII character to decimal number in Android?
提问by adamhala007
I'm working on an Android app. I have a TextView which contains an ASCII character. This character changes every 2 hours. I need to be able to read this character and convert it to decimal number, and then write it to an another TextView. So let's say the character is "[" and it's decimal value is 91. 2 hours later this character changes to "U" and it's decimal value is 85.
我正在开发一个 Android 应用程序。我有一个包含 ASCII 字符的 TextView。此角色每 2 小时更改一次。我需要能够读取这个字符并将其转换为十进制数,然后将其写入另一个 TextView。所以假设字符是“[”,它的十进制值为 91。2 小时后,这个字符变为“U”,它的十进制值为 85。
Can anyone help me what kind of code should I use in my app to be able to convert ASCII character to decimal number?
任何人都可以帮助我在我的应用程序中使用什么样的代码才能将 ASCII 字符转换为十进制数?
Thanks for helping.
谢谢你的帮助。
回答by TN888
You can get chars in loop like that :
您可以像这样在循环中获取字符:
char x;
int[] t = new int[string.length];
for(int i = 0; i < string.length; i++)
{
x = string.charAt(i);
int z = (int) x;
t[i] = z;
}
回答by FD_
Use this if your String has a length of 1:
如果您的字符串长度为 1,请使用此选项:
char c = yourString.charAt(0);
int decVal = (int) c;
回答by Rahul kushwah
String str = "123";
int size = str.length();
int [] arr = new int[size];
for(int i =0; i<size;i++) {
arr[i] = Character.getNumericValue(str.charAt(i));
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
output:
输出:
123
123
Simple way is
简单的方法是
int a = Integer.parseInt(str); // parsing