wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Converting string to ASCII values and back Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Converting string to ASCII values and back" Watch "Converting string to ASCII values and back" New topic
Author

Converting string to ASCII values and back

Ryan de Kwaadsteniet
Greenhorn

Joined: Sep 03, 2012
Posts: 4
Hello, i am trying to basically take a string entered by the user, convert each character into their ASCII value, and then take that string of numbers (eg. hello = 104101108108111) and change it back to a character. Ive been having problems with the decoding part though. i cant seem to single out the numbers in the string (eg. 104 = h). Would you know how to do that and would i have to use Arrays?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

First - Welcome to the Ranch.

First, it would help if you would UseCodeTags (<---that's a link) when posting your source code.

Next, if you need help, you have to ask. Tell us what the code DOES, and what you EXPECT it to do.

Finally, you may want to read our FAQ on how to get the most out of our site: HowToAskQuestionsOnJavaRanch


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

Joined: Sep 03, 2012
Posts: 4
Hello, i am trying to basically take a string entered by the user, convert each character into their ASCII value, and then take that string of numbers (eg. hello = 104101108108111) and change it back to a character. Ive been having problems with the decoding part though. i cant seem to single out the numbers in the string (eg. 104 = h). Would you know how to do that and would i have to use Arrays? I hope this is more informative.



Guillaume Jourdan
Ranch Hand

Joined: Jul 24, 2012
Posts: 47

Hi,

You want to convert UTF String to ASCII String?

example :
Ryan de Kwaadsteniet
Greenhorn

Joined: Sep 03, 2012
Posts: 4
I have changed my code completely but know face a problem where char[] is required but i have char and where char[] is found but i need int. May you please help me out with how to change them?

Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Guillaume Jourdan wrote:Hi,

You want to convert UTF String to ASCII String?

example :


This piece of code does not make much sense!

First you convert a String using the ASCII encoding into a byte array, and then assume this byte array to be the platform-default encoded version of a String and fetch it.

There is no such thing like an UTF String or an ASCII String. There are Strings and they can (or can not) be represented using several encodings as byte arrays.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Ryan de Kwaadsteniet wrote:Hello, i am trying to basically take a string entered by the user, convert each character into their ASCII value, and then take that string of numbers (eg. hello = 104101108108111) and change it back to a character.

Well first, I assume this is an exercise for a class, because otherwise it's rather pointless.

If I understand you right, the first thing to understand is that BOTH forms are Strings, so I suspect that you want to convert the String "hello" to a String that contains each character of "hello" converted to its 3-digit decimal number (0-filled if need be) in String form.

There are several ways of doing it, but my suggestion would be to look at String.format().

Given a character (let's say 'h'), the expression
String.format("%03d", (int) 'h')
will return 'h's value as a decimal String with at least 3 digits (0-filled if necessary).

So a method:will return a String of the format you want.

Now see if you can put that to some use for your encryption.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ryan de Kwaadsteniet
Greenhorn

Joined: Sep 03, 2012
Posts: 4
I restarted it but i am now having trouble how to store the chars in an array. How would i do so?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

it is not clear at all what you are trying to do. You are putting too much code in your main method. you need to slow down and write down your algorithm in english. don't just write a bunch of code and say "hmm...this doesn't work. I'll start over".

You should break down your code into discrete methods. a method to read the data. A method to parse the data. A method to print data. etc.

variable should have descriptive names. what does the variable 'a' represent - apples? aardvarks? atomicWeapons? you aren't texting someone - use your keyboard and make life easy on yourself (and everyone else who is trying to help you).
 
 
subject: Converting string to ASCII values and back
 
Similar Threads
Help with english to morse code program, I have no idea what I'm doing
Counting one character at a time from a file
Battleships - guys please help me out!
determining an input's data type.(part 2)
Part of program not printing output