• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting ASCII code to characters  RSS feed

 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to find a class that will convert ASCII codes to characters.

For example it should convert the following ASCII codes: 76, 101, 116 to the characters Let

Any help would be much appreciated.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do the values come in?

If they're numbers then just stick them in a char[].
If they're Strings (eg "76") then parse them and then stuck them in a char[].
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To expand on what Dave Tolls said, try this:
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And a minor point: what they wrote will work for all Unicode characters. If you really want to restrict the code to working only on the first 128 characters (which is what ASCII means) then you'll have to do something extra.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave, Fred and Paul

Thanks to you all for your speedy replies. I can't believe that its as simple as that, but then this must surely have been one of the fundamental considerations when Java was first written.

Paul - re. your comment about Unicode and ASCII, I suppose it made sense to the creators of Unicode to simply extend the ASCII codes and keep the same first 128 numbers. This question was sparked by (would you believe) a job advert, which simply gave a string of codes, which converted into a message. I assumed that they were ASCII codes, but they could equally have been Unicode.

The Sinclair ZX Spectrum microcomputer of 1980's vintage actually had a 256 code 'character' set. Codes 0 to 5 were not used, and every key press or combination of key presses, on the Spectrum keyboard equated to an ASCII code. So for example the BASIC GO TO command was ASCII code 236, etc.. Of course with the advent of Unicode, this code will generate something different, when put into a char in Java...
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Lord wrote:So for example the BASIC GO TO command was ASCII code 236, etc.. Of course with the advent of Unicode, this code will generate something different, when put into a char in Java...

236 may have been a "code" but it wasn't ASCII. The range of ASCII is from 0 to 127 inclusive.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something called "extended ascii", but it is a) poorly named, b) not uniform and c) often confused for ASCII.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred and Carey - thanks for your input on this topic.

I am inclined to agree with Fred when he mentions "extended ASCII"

This article in Wikipedia clarifies the matter when it describes the ZX Spectrum charater set as a
variant of ASCII
Wikipedia - ZX Spectrum character set

Incidentally Fred Kleinschmidt's example in Java:


can be accomplished in 1 statement in Sinclair BASIC:
which gives "£"

Naturally I prefer Java over BASIC
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is even quicker in Forth
97 EMIT aok
Remember the ok is the prompt not part of the output, so the output is a. How you get a pound sign from 96 I cannot imagine. I presume it is 96 in hex.

Back to the question in hand: have you worked out how to take 0x96 or even better 0xc2 and get a pound sign displayed?
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell - thanks for your input. To be brief and to answer your question, the following code, will display a dollar sign, (36) but not pound sign (96). In fact you get a ' instead, but I seem to recall that originally the £ sign wasn't even shown on some keyboards.



What I would like to do is to parse the first argument in the command line into the char - as per Dave Tolls second suggestion - but can't see how this is done.

Any suggestions or pointers would be welcome.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a String and you want to parse it into an int value then Integer.parseInt() is the method you're looking for.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Paul

Thanks for your help. I had already tried your suggestion and the following code



gives this error on compilation:

CodeDisplay.java:6: error: incompatible types: possible lossy conversion from int to char
char c = codevalue;
^
1 error


should I be casting the int as a char and if not how do you parse the value in the int named codevalue into a char?

I look forward to your comments.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a downcast:

BTW, that's a one-liner, too.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason the downcast works is because a char is an integer type. Technically, you're doing a narrowing conversion of primitive types.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu

Thanks for your input - the change to the System.out.println statement gives the desired result.

The code now reads



So when you downcast an int into a char and display it, you get the character which that code represents. Bearing in mind that the number 1 is represented by code 49 (Let me know when your head stops spinning! ).

As for one liners - I would say that is more obvious, as to what the computer is doing, over against , but then maybe others would like to give their opinions...

I'll develop the class so that it does the same for an array of values - in somewhat slower time
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Lord wrote:As for one liners - I would say that is more obvious, as to what the computer is doing, over against , but then maybe others would like to give their opinions...

Perhaps. Or you could see it as simply as the difference between TO-MAY-TOH and TO-MAH-TOH.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't like the downcast, you can also do this:

or

Do the latter only if you have a small, focused class and many instances of Character.toChars() such that importing the static method can help simplify the code considerably. Note that the return value of Character.toChars() is a char[].
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Junilu

Thanks for your input and the 2 alternative ways of putting the value in codevalue into a char.

I have now developed the class - as I mentioned earlier - so that it reads in an array of values from the command line.

The following class



when given the command line values:
76 101 116 32 116 104 101 32 98 101 115 116 32 115 116 97 114 116 117 112 115 32 97 112 112 108 121 32 116 111 32 121 111 117

outputs : Let the best startups apply to you

I have one final question - when I use an enhanced for loop in line 10, thus:

the compilation fails with the message: CodeDisplayPlus.java:10: error: incompatible types: String cannot be converted to int
for (int x : args)
^
1 error


Why does the enhanced for loop not compile, when both versions of the class access the args array and the control variable and the codevalues array have not been renamed?

I would hazard a guess that it is actually Integer.parseInt that it isn't happy with the enhanced for loop.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be



because "args" is an array of Strings.

Doing it that way has the disadvantage that you just get the String out of the array and you don't get the array index number, which in your case you need.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

Thanks for your help - its clear to me that the enhanced for loop will not work in this situation.

Re. your earlier comment,
And a minor point: what they wrote will work for all Unicode characters.
yes, I agree that the code:
public class SingleCodeDisplay


outputs the £ sign - but only if you use a decimal value. If you substitute 163 for its hex equivalent - A3, then you get the compilation error message:
SingleCodeDisplay.java:5: error: cannot find symbol
char c = A3;
^
symbol: variable A3
location: class SingleCodeDisplay
1 error


so I will find a way of converting hex to decimal...

Incidentally, I notice that the old ASCII value for a £ - 96 - is now 136 or A3 in Unicode.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If A3 is in the String as a number, then parseInt has an overloaded method with a second parameter for the radix.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Lord wrote:Incidentally, I notice that the old ASCII value for a £ - 96 - is now 136 or A3 in Unicode.


That was never the ASCII value for £; in ASCII code point 96 represents the ` (backwards quote) character. As Wikipedia's article about ASCII says:

Wikipedia wrote:As computer technology spread throughout the world, different standards bodies and corporations developed many variations of ASCII to facilitate the expression of non-English languages that used Roman-based alphabets. One could class some of these variations as "ASCII extensions", although some misuse that term to represent all variants, including those that do not preserve ASCII's character-map in the 7-bit range. Furthermore, the ASCII extensions have also been mislabelled as ASCII.


So presumably you're referring to one of those variants which didn't preserve the characters in the 7-bit range. But in practice all of those things are ancient history in the Java world, since Unicode is Java's native character set.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul - thanks for your observation. I actually refered to the Sinclair ZX Spectrum character set, which is described in another Wikipedia article as a variant of ASCII. So it seems that I've confused an ASCII variant with ASCII...!

Microcomputers are a relic of the 1980's and early 1990's, but machines like the ZX Spectrum did fuel an interest in computer programming and home computing. This, I contend lead indirectly to the World Wide Web and the creation of Java as a specifically internet-oriented language. Of course once you have computers being used by speakers of many of the worlds languages, then it isn't hard to see why Unicode came along to accomodate the many different character sets in use.

Interestingly our current society has recognised the value of encouraging interest in computing, with the Raspberry PI being used in schools today - which is what the BBC B Micro attempted to do in the 1980's...

Dave - thanks for your further help. So the lesson is: if you want to display a hex value as a char, then it must be prefixed by 0x.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart Lord wrote:
Dave - thanks for your further help. So the lesson is: if you want to display a hex value as a char, then it must be prefixed by 0x.


Not quite (unless I'm misreading your sentence).
If you want to have a hex value in your source code then it has to be prefixed with '0x'. That tells the compiler that the value after it is a hexadecimal integer.
Similarly if you wanted it to appear in source as a binary then it needs to be prefixed by '0b'.

Neither of those are to do with parsing a value in a String.
 
Stuart Lord
Ranch Hand
Posts: 40
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave - thanks once again for your help and observations. So to summarise:

* Use 0x or 0b in source code in front of a value - e.g. 0xA3 - this tells the compiler to treat the value after it as a Hexadecimal integer or if 0b as a Binary integer.
* This code merely takes a String value from the command line and parses it into an int, then displays it as an integer value.
If you want to get the character representation of that int then you have to downcast it into a char thus:
* In order to cope with Hex values in the command line, use instead. This parses all the values in the command line into the codeValues array. Bearing in mind that Hex 76 is Decimal 118 and the character representation of that is v, whereas the character representation of Decimal 76 is L..
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!