简体   繁体   中英

How to convert ASCII character to decimal number in Android?

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.

Can anyone help me what kind of code should I use in my app to be able to convert ASCII character to decimal number?

Thanks for helping.

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;
}

Use this if your String has a length of 1:

char c = yourString.charAt(0);
int decVal = (int) c;
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

Simple way is

int a = Integer.parseInt(str); // parsing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM