This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes String to int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "String to int" Watch "String to int" New topic
Author

String to int

knot Ade
Greenhorn

Joined: Jul 20, 2001
Posts: 2
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


K<BR>O O<BR> L<BR> A
Ganesh Ram
Ranch Hand

Joined: Jul 20, 2001
Posts: 33
You should convert your string to a byte array.

------------------
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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>


"I'm not back." - Bill Harding, Twister
knot Ade
Greenhorn

Joined: Jul 20, 2001
Posts: 2
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>

 
 
subject: String to int
 
Similar Threads
JSTL: EL and variable names with "."
Facing problem to set classpath
Assigning values to variables in constructors
A Baffling Question
Assigning a array of array to an Object reference