ASCII Chars

S

SamMan

This is a *very* simple question, I know, but how would you either display,
or check for a value by using an ASCII character?

Thanks,
Sam
 
M

Marco Schmidt

SamMan:
This is a *very* simple question, I know, but how would you either display,
or check for a value by using an ASCII character?

Just compare if the character falls into the right interval. Values 0
to 127 are ASCII, but the first 32 are non-printing
<http://www.asciitable.com/>. So this may be what you want:

public static boolean isAscii(char c) {
return c >= 32 && c < 128;
}

Maybe

return (c & 224) == 96;

works as well... Can't think, must eat first. :)

Regards,
Marco
 
J

Jon A. Cruz

SamMan said:
This is a *very* simple question, I know, but how would you either display,
or check for a value by using an ASCII character?


What exactly do you mean by "ASCII character?"

I ask because it's often a misused label, and with Java it does indeed
make a difference.
 
J

John C. Bollinger

Marco said:
SamMan:

This is a *very* simple question, I know, but how would you either display,
or check for a value by using an ASCII character?
[...]

Maybe

return (c & 224) == 96;

works as well... Can't think, must eat first. :)

Nope. (How about c == 65 ?). I don't see how to do it with a single
bitwise operation, but I think this would work:

return (((c & 160) == 32) || ((c & 192) == 64));

You could also try

return ((c & 224) ^ 96) != 96;

if that appeals to you.


John Bollinger
(e-mail address removed)
 
S

SamMan

Jon A. Cruz said:
What exactly do you mean by "ASCII character?"

I ask because it's often a misused label, and with Java it does indeed
make a difference.

Sorry... I should have said ASCII value. As in the Dec, Hx or Oct value of,
say #.

Sam
 
J

Jon A. Cruz

John said:
Marco said:
SamMan:

This is a *very* simple question, I know, but how would you either
display,
or check for a value by using an ASCII character?

[...]

Maybe
return (c & 224) == 96;

works as well... Can't think, must eat first. :)


Nope. (How about c == 65 ?). I don't see how to do it with a single
bitwise operation, but I think this would work:

[SNIP]

Uh oh.


You just mixed the concept of encoded character with language
conversions. Might not always work properly.
 
J

Jon A. Cruz

SamMan said:
Sorry... I should have said ASCII value. As in the Dec, Hx or Oct value of,
say #.


Again, that sounds like you are not using the term correctly.


Let's try two different expansions:


A)
how would you either display, or check for a value by using a 7-bit
value ranging from 0 through 127 that is only mainly good for US
English, Hawaiian, Swahili and Latin value?


B)
how would you either display, or check for a value by using the numeric
value of some character?



I think the latter is what you mean.


Remember, MS Windows does not do "ASCII". It does "ANSI". Java does not
use ASCII, it uses Unicode, which has 16-bit unsigned values ranging
from 0 through 65536.

There is no ASCII value for 'ñ', nor for 'µ', '±', 'ß', '£', '¥', '¢'...

However, there are Latin-1 values, Unicode values, Cp1252 values and
MacRoman values for all of those.
:)



Now, if you mean "numerical value" and not "ASCII", then it's simple.

Java characters are 16-bit unsigned integers.

int i = 'A';
char c = 65;
char c2 = 'A'

if ( 'A' == i )
....
 
S

SamMan

Jon A. Cruz said:
Again, that sounds like you are not using the term correctly.

snip...


Thanks for your help. I just realized (I think) that I would be trying to
compare a substring to a char, in this case the numerical value of one. I
would have to cast the substring to a char before making the comparison,
right? If this is the case, it would be simpler, less confusing and less
code to just compare Strings.

Sam
 
J

Jon A. Cruz

SamMan said:
Thanks for your help. I just realized (I think) that I would be trying to
compare a substring to a char, in this case the numerical value of one. I
would have to cast the substring to a char before making the comparison,
right? If this is the case, it would be simpler, less confusing and less
code to just compare Strings.

Depends on what the *meaning* of what you are trying to do is.

First of all, a numerical value of 1 is very suspicious. Sounds like
you've brought over the bad C/C++ habit of mixing byte data in strings.

Is the information you have truely a sequence of characters? Or is it a
sequence of byte values? If the former, use a String; if it is the
latter, use a byte[].


You can't cast a substring to a char, however you can call several handy
methods on the string, including indexOf().

Check the API docs:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html


Remember, if you convert to or from bytes, BEWARE!!!! That's veeeeery
easy to have corrupted data in doing so.
 
T

Tukla Ratte

Marco said:
SamMan:

This is a *very* simple question, I know, but how would you either display,
or check for a value by using an ASCII character?
[...]

Maybe

return (c & 224) == 96;

works as well... Can't think, must eat first. :)

Nope. (How about c == 65 ?). I don't see how to do it with a single
bitwise operation, but I think this would work:

return (((c & 160) == 32) || ((c & 192) == 64));

You could also try

return ((c & 224) ^ 96) != 96;

if that appeals to you.

Oy vey.

If I ran across any code like that without some sort comment
explaining what it's for, someone would have to die. >:cool:
 
J

Joona I Palaste

Jon A. Cruz said:
Again, that sounds like you are not using the term correctly.
Let's try two different expansions:
A)
how would you either display, or check for a value by using a 7-bit
value ranging from 0 through 127 that is only mainly good for US
English, Hawaiian, Swahili and Latin value?
B)
how would you either display, or check for a value by using the numeric
value of some character?
I think the latter is what you mean.
Remember, MS Windows does not do "ASCII". It does "ANSI". Java does not
use ASCII, it uses Unicode, which has 16-bit unsigned values ranging
from 0 through 65536.
There is no ASCII value for 'ñ', nor for 'µ', '±', 'ß', '£', '¥', '¢'...
However, there are Latin-1 values, Unicode values, Cp1252 values and
MacRoman values for all of those.
:)

I know at least one person who advocates the use of "ASCII" to mean the
very concept of organising character glyphs in a deterministic countable
order.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"That's no raisin - it's an ALIEN!"
- Tourist in MTV's Oddities
 
J

Jon Skeet

Joona I Palaste said:
I know at least one person who advocates the use of "ASCII" to mean the
very concept of organising character glyphs in a deterministic countable
order.

On what grounds?
 
J

Joona I Palaste

On what grounds?

He didn't say. But he flamed me for correcting his post where he
referred to UTF-8 as ASCII.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"B-but Angus! You're a dragon!"
- Mickey Mouse
 
J

Jon A. Cruz

Joona said:
He didn't say. But he flamed me for correcting his post where he
referred to UTF-8 as ASCII.

Point things out next time you have problems.

Among other things, "ASCII" is pretty well defined by a few standards.
And it even seems to be used correctly by the RFC's. :)
 
R

Roedy Green

If the legal values are unaccented, you can use a rough bounds check
followed by a lookup in a BitSet or boolean[theChar-lowerlegalBound]
to determine if the character is legal. If there are a handful of
legal chars, you can use a CASE, and let the compiler figure out how
to optimise it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top