• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String to int

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should convert your string to a byte array.

------------------
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>


 
Because those who mind don't matter and those who matter don't mind - Seuss. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic