user3206384
user3206384

Reputation: 29

String to decimal conversion

I am new to Android. I want covert String to Decimal value e.g.

String str = "ABC"

I need result 65 66 67 How can get this result?

Thanks in advance

Upvotes: 0

Views: 97

Answers (5)

Dhrumil Shah - dhuma1981
Dhrumil Shah - dhuma1981

Reputation: 15789

Use below method stringToAscii()

public static long stringToAscii(String s){
        StringBuilder sb = new StringBuilder();
        String ascString = null;
        long asciiLong;
                for (int i = 0; i < s.length(); i++){
                    sb.append((int)s.charAt(i));
                    char c = s.charAt(i);
                }
                ascString = sb.toString();
                asciiLong = Long.parseLong(ascString);
                return asciiLong;
    }

Upvotes: 0

Mohammad Arman
Mohammad Arman

Reputation: 7065

Try this:

String abc = "abc";
        for(int i = 0 ;i<abc.length()-1;i++){
            abc.substring(i,i+1);
            char a = 'a';
            int ascii = (int) a;
            System.out.println(ascii+" ");
        }

Upvotes: 0

Exception
Exception

Reputation: 2323

It is very simple, its more of a java question compared to android

String str = "ABC";
byte[] bytes = str.getBytes("US-ASCII");

Upvotes: 1

Sushant kunal
Sushant kunal

Reputation: 421

Run a loop like following

String str = "ABC";
char[] cArray = str.toCharArray();
for(int i=0; i< cArray.length(); i++){
    int ascii = (int) character;
    System.out.println(ascii );//Will print ASCII value
}

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 6169

You can get it from character, use this:

char ch = 'A';    
int ans = (int) ch;

for String you can use:

String mString = "ABC";    
int ansA = (int) mString.charAt(0);
int ansB = (int) mString.charAt(1);
int ansC = (int) mString.charAt(2);

Upvotes: 0

Related Questions