Welcome to the TechSpot OpenBoards. Please read the FAQ if you have any questions. Login to participate.

Go Back   TechSpot OpenBoards > OS & Software > Misc. Software & Utilities

Java problem (filling an array with the ascii table)

Reply
Bookmark / Share this page
Thread Tools
  #1  
Old 05-10-2004
MrGaribaldi's Avatar
TechSpot Ambassador
 
Location: Babylon 5, Grid Epsilon
Member since: Feb 2002, 2,802 posts
Java problem (filling an array with the ascii table)

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

Last edited by MrGaribaldi; 05-10-2004 at 05:28 PM.
Reply With Quote
  #2  
Old 05-11-2004
Nodsu's Avatar
TS Special Forces
 
Location: Estonia
Member since: Feb 2002, 9,430 posts
System specs
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..
Reply With Quote
You can remove this banner by registering, join the TS Community for free.
  #3  
Old 05-11-2004
Nic's Avatar
Nic Nic is offline
TechSpot Paladin
 
Location: UK
Member since: Jan 2003, 1,920 posts
Try this ...

char c = Character.forDigit (myInt, 10);
// base 10 used for radix value
Reply With Quote
  #4  
Old 05-11-2004
MrGaribaldi's Avatar
TechSpot Ambassador
 
Location: Babylon 5, Grid Epsilon
Member since: Feb 2002, 2,802 posts
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)
Reply With Quote
  #5  
Old 05-12-2004
Nodsu's Avatar
TS Special Forces
 
Location: Estonia
Member since: Feb 2002, 9,430 posts
System specs
Oh.
I misunderstood your problem then..
You might as well do System.out.print((char)101); then.
Reply With Quote
  #6  
Old 05-15-2004
MrGaribaldi's Avatar
TechSpot Ambassador
 
Location: Babylon 5, Grid Epsilon
Member since: Feb 2002, 2,802 posts
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];
            }
Reply With Quote
  #7  
Old 11-24-2005
Newcomer, in training
 
Member since: Nov 2005, 1 posts
hi every body!
perhaps this code will be helpfull for you! i found it here:http://www-128.ibm.com/developerwork...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:
Reply With Quote
You can remove this banner by registering, join the TS Community for free.
Reply
Thread Tools

Forum Jump

Similar Topics
Thread Thread Starter Forum Replies Last Post
Recommendations for RAID 0 Stripe Size Phantasm66 Storage & Networking 17 08-16-2006 09:33 PM
Erratic problem, sometime no boot-picture Per Hansson CPUs, Chipsets and Mobos 3 10-21-2003 06:38 AM
Sun suing MS - commentary lokem News & Interesting links 7 03-21-2002 06:21 AM


All times are GMT -4. The time now is 06:43 AM.