File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes converting ascii number to capital letter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "converting ascii number to capital letter" Watch "converting ascii number to capital letter" New topic
Author

converting ascii number to capital letter

Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
I generated a random number using currentTimeMillis and I want to convert this to a capital letter equal to the value of the random number. Can someone help me figure this out ?
Thanks
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Why would a random number correspond to a capital letter?


The code above casts the int value 65 to a char, which converts it to an 'A', since 65 is the ASCII value for the 'A'. currentTimeMillis returns a large number (of milliseconds since the epoch) which is not going to correspond to any capital letter

See here for info on the ASCII to character mapping.


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 150

Let's try it...
currentTimeMillis probably returns an int. Convert int to Integer, then grab the string from the Integer. Once you have the string, look at the API from String and see if any of those can help you.

Hope this helps,
Gary


star chaser..
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.


Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Ken and welcome to the JavaRanch.

System.currentTimeMillis() returns a long and that has a much larger range then letters. So you'll need to reduce the range.
Look at the Character class's methods. You'll find a useful method.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
pete stein wrote:
Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.


Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!


I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.
Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
Ken Mullins wrote:
pete stein wrote:
Ken Mullins wrote:I want to convert this to a capital letter equal to the value of the random number.


Hello. Can you clarify the statement above? For instance, what capital letter is equal to 2302?

Also, what have you tried so far? Can you post your code?

Best of luck!

Oh, and welcome to the Ranch!


I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.

public class RandomCharacter
public static void main(String[] args)
long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;
long currentSecond = (int)(totalSe3conds % 60);
double asciiNew = currentSecond;
System.out.print(asciiNew);

This generates a 2 digit number that I want to be cpnverted to a capital letter

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.


You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter -- not the code for this, but rather the logic for this -- as there is no one standard way to do this. Your post is still not clear on this point. Or perhaps it would help us if you told us just what you were going to do with the capital letter once obtained. In other words, what is the ultimate goal of this exercise?

Best of luck!
Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
pete stein wrote:
Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.


You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter. Your post is still not clear on this point.

Best of luck!
The code generates a random 2 digit number. I want to convert this number using the Ascii or unicode to the corresponding letter. Am I going down the wrong road here ? I am new at this but I am enjoying it.

Thanks for your help !
Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
pete stein wrote:
Ken Mullins wrote:
I have converetd the number returned by using current time in milliSeconds and have generated 2 2 digit number. I would like to convert this number to a capital letter.


You still will want to describe to us in one or several sentences just how you intend to convert a number to a letter -- not the code for this, but rather the logic for this -- as there is no one standard way to do this. Your post is still not clear on this point. Or perhaps it would help us if you told us just what you were going to do with the capital letter once obtained. In other words, what is the ultimate goal of this exercise?

Best of luck!
The goal here is simply to display the letter.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

The problem is not how to do it, but how YOU want to do it. if you are generating a random 2-digit number, you're getting values from 00-99. which of those numbers should be displayed as what capital letter?

suppose you get a random number of 12 - what capital letter should it be?

If you want to use ASCII, you would need to generate numbers between 65 and 90 only. 12 is the ASCII code for a Form Feed - which is not any capital letter.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ken Mullins
Greenhorn

Joined: Sep 25, 2010
Posts: 25
fred rosenberger wrote:The problem is not how to do it, but how YOU want to do it. if you are generating a random 2-digit number, you're getting values from 00-99. which of those numbers should be displayed as what capital letter?

suppose you get a random number of 12 - what capital letter should it be?

If you want to use ASCII, you would need to generate numbers between 65 and 90 only. 12 is the ASCII code for a Form Feed - which is not any capital letter.
I did not think about that. So, I guess I would need to generate a random numer within a range of 65 to 90 and then display that number. Can I designate that this letter be a capital ?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Ken Mullins wrote:
I did not think about that. So, I guess I would need to generate a random numer within a range of 65 to 90 and then display that number. Can I designate that this letter be a capital ?


Now this is do-able. You can convert an int to a char simply by casting it:

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

if you look at the ascii table, you will see that by definition, 65-90 IS a capital letter.

Lower case is defined as 97-122.

To get a range of 65-90, you need to figure out two things - a range and an offset. for example, if i wanted the values 6-10, that is a range of 5 possible values (6,7,8,9 and 10). So, I really need to generate a random number from 0-4, and then 'translate' them to 6-10. That's the offset. 0 maps to 6, 1 maps to 7, etc. each values needs to be 'offset' by 6. So, if i generate values from 0-4, and then add 6 to each, i'll have what I want.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: converting ascii number to capital letter