• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting string to ASCII values and back

 
Ryan de Kwaadsteniet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan de Kwaadsteniet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You want to convert UTF String to ASCII String?

example :
 
Ryan de Kwaadsteniet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ryan de Kwaadsteniet
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic