Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting an array of ints into ascii  RSS feed

 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 8-9 digit integers generated as one unit, Is there a way to convert these into ascii? They're from an array.
It doesn't quite work.


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you want to print the textual form of a number, so for example to turn the number 123 into the String "123"?

If so check out the Integer class. It has a method that will do what you want.

You don't seem to be using your array though, what is it for?

Also, arrays start at index 0, so if that for loop is for operating over the array it will have to be for indexes 0-9, not 1-10.
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I assume you want to print the textual form of a number, so for example to turn the number 123 into the String "123"?
. . ..


I want the int 123 to be converted into ascii 049 050 051, and I've heard I have to use a char for it.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A String is a collection of chars, so look in the Integer class for the method that will give you a String. If you really need the chars then you can get them from the String, but if all you want to do is print the number then you don't need the chars.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what are you doing with the digits array?

So you want to get the ASCII values of the individual digits? I think you might do well to turn the entire number into a String and look at its individual chars. Remember that a char is a number already.
Don't write a number as 049 or 050 because those don't mean 49 and 50.
 
Gabe Evan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what are you doing with the digits array?
. . .


The digits array generates 10 random numbers. I was hoping I wouldn't have to do them individually but I guess I do.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gabe Evan wrote: . . .
The digits array generates 10 random numbers. I was hoping I wouldn't have to do them individually but I guess I do.
You mean the Random object generates 10 numbers.

And please tell us what the smallest and the largest numbers you might generate are.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!