Java problem (filling an array with the ascii table)

Status
Not open for further replies.

MrGaribaldi

Posts: 2,488   +1
Hello :)

I've stumbled onto a problem on my latest assignment at the university, and hopefully someone can help me out...

The assignment says to create an array with 256 slots, which shall be filled with lists. The first node of each list shall be a char from the ascii table .

Now I know the first 32 entries on the ascii table are non-printing characters, and thus could be skipped, but the assignment calls for an array with 256 slots, so I want to fill them.

Now what my problem is:

How can I convert an int to a String with the corresponding ascii character?
Ie. "101" to "e", "173" to "¡".

I know that using the following code will convert from characters to the corresponding ascii number, but I can not fathom how to convert it back
Code:
        test  = Integer.getInteger(readChar);
        test = new Integer(readChar);
//can use either of the methods above
        value = test.intValue();

(Also any streamlining of the above code will also be welcomed :))

My plan is to use the integer value in the array (due to the first 32 characters) and use .charAt(0) (converted to integer using (some of) the above code) to see where in the array to search for the word).

I know by doing that I don't need to convert back to characters, but I'll need characters for a full printout of the array so....

Any help is appreciated!
TIA :)
 
You want to print out a number in human readable form?

The String class contains static functions to convert numbers.

You would do sth like:
myString=String.valueOf(myByte);
System.out.println(myString);

I am not responsible for syntax errors. It's not like I write code every day..

Or maybe I understood your question wrong..
 
Try this ...

char c = Character.forDigit (myInt, 10);
// base 10 used for radix value
 
Thanks for your input people :)

Alas, I could get neither of your suggestions to work.
But searching for what a "radix value" is, I came across the answer I was looking for...

all I had to do was this
Code:
int number = 101;
char c = (char) number;
System.out.print(c);

Alas, doing that work, so now I have no more bad excuses to avoid working on the assignment...
(Been in a foul mood today, only playing Bf1942)
 
Oh.
I misunderstood your problem then..
You might as well do System.out.print((char)101); then.
 
Yeah... that might've worked too...

here's (part of) what the final result became...
Code:
            firste = newWord.charAt(0);
            place = (int) first;
            
            if(wordList[place] == null){
                wordList[place] = new Word(newWord, spam, null);
                return;
            }else {
                next = wordList[place];
            }
 
hi every body!
perhaps this code will be helpfull for you! i found it here:http://www-128.ibm.com/developerworks/java/library/j-mer1022.html

import java.nio.*;
import java.nio.charset.*;
import java.util.Arrays;

public class Convert {
public static void main(String args[]) {
System.out.println(Charset.availableCharsets());
Charset asciiCharset = Charset.forName("US-ASCII");
CharsetDecoder decoder = asciiCharset.newDecoder();
byte help[] = {72, 101, 108, 112};
ByteBuffer asciiBytes = ByteBuffer.wrap(help);
CharBuffer helpChars = null;
try {
helpChars = decoder.decode(asciiBytes);
} catch (CharacterCodingException e) {
System.err.println("Error decoding");
System.exit(-1);
}
System.out.println(helpChars);
}

bye :knock:
 
Status
Not open for further replies.
Back