• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to int

 
knot Ade
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to covert a String to its ascii value. but nothing seems to work.
I tried:
int value = Integer.valueOf(c).intValue();
where c is a String object or variable.
I also tried:
int value = (int)c;
where c is a char.
They both don't work, i get errors saying can not cast String to int.

------------------
K
O O
L
A
 
Ganesh Ram
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should convert your string to a byte array.

------------------
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String doesn't have an ASCII value. It has a series of characters which have ASCII values. (I assume by ASCII value you mean numeric value; technically it's only ASCII if it's in the range 0-127, or 0-255 for one of various extended ASCII forms.) The simplest way to get this is (copied from a neighboring thread):
<code><pre>
public static void showCharValues(String input) {
int length = input.length();
for (int i = 0; i < length; i++) {
char c = input.charAt(i);
System.out.println("Character " + i + " = " + (c && 0x0000FFFF)
+ " (hex " + Integer.toHexString(c) + ") = (\'" + c + "\')");
}
}
</pre></code>
 
knot Ade
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim, it worked.
Originally posted by Jim Yingst:
A String doesn't have an ASCII value. It has a series of characters which have ASCII values. (I assume by ASCII value you mean numeric value; technically it's only ASCII if it's in the range 0-127, or 0-255 for one of various extended ASCII forms.) The simplest way to get this is (copied from a neighboring thread):
<code><pre>
public static void showCharValues(String input) {
int length = input.length();
for (int i = 0; i < length; i++) {
char c = input.charAt(i);
System.out.println("Character " + i + " = " + (c && 0x0000FFFF)
+ " (hex " + Integer.toHexString(c) + ") = (\'" + c + "\')");
}
}
</pre></code>

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic