• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem related to Ascii Value

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have String and int value which is nothing but ascii value for particular character. I want to search that character in String.
Problem is my int variable will have always value of any character A-Z means 65 to 90, suppose I have 65 and my string do not have 'A' in it,but has 'a'.Then I want to have index of 'a'.
How should I do this?
Is any java class gives me character corresponding to given int value..

Thanx in advance..
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can convert an int to char with an explicit cast: char ch = (char) intvalue;

The lowercase letter is: char ch = (char) (intvalue + 32);
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Converting between integers and characters, simply by casting, is rather dodgy, unless you are really sure that your integer can only represent a character in the 7-bit ASCII set. Might your application ever be internationalised? If so, you should seek a better approach.

Also, converting between upper and lower case by adding 32 to the integer value is bad, for similar reasons. Use Character.toLowerCase() and Character.toUpperCase(). These work correctly for international characters. They are also clearer to the reader of your code.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic