Character Operators

  • Thread starter Thread starter Fred Chateau
  • Start date Start date
F

Fred Chateau

I found the following code in an online tutorial, and I'm having some
difficulty understanding it. Unfortunately, it did not list any expected
output examples for val.

What type is val? Is this statement comparing, adding, and subtracting ASCII
numbers?

if ('0' <= ch && ch <= '9') val = ch - '0';
else if ('A' <= ch && ch <= 'Z') val = 10 + ch - 'A';
else {val = 0; Console.WriteLine("invalid character {0}", ch);}
 
That was the entire example, showing a simple example of a For statement.

The context is not relevant to the question I am asking, which is, what is
the result of subtracting 10 - 'Z'? In other words, are the single quotes
here some sort of shorthand for converting characters to ASCII numbers?

I do not see how this has anything to do with hexadecimal (or any other
base) numbers.
 
Fred said:
I found the following code in an online tutorial, and I'm having some
difficulty understanding it. Unfortunately, it did not list any
expected output examples for val.

What type is val? Is this statement comparing, adding, and
subtracting ASCII numbers?

if ('0' <= ch && ch <= '9') val = ch - '0';
else if ('A' <= ch && ch <= 'Z') val = 10 + ch - 'A';
else {val = 0; Console.WriteLine("invalid character {0}", ch);}

This converts digits in any base from 2 to 36 to internal binary.
Characters '0'-'9' are converted to 0-9, and 'A'-'Z' are converted to
10-35.

So for characters ranging from '0' and '9', you simply subtract the
ASCII value of '0' to get 0-9. For 'A'-'Z', you subtract the ASCII
value of 'A' and then add 10.

--
Rudy Velthuis http://rvelthuis.de

"I think it would be a good idea."
-- Mahatma Gandhi (1869-1948), when asked what he thought of
Western civilization
 
Peter said:
The example didn't show the declaration of "val"? Doesn't sound
like
much of an example to me then.


Of course it's relevant. The example is intended to demonstrate
_something_. Without knowing what that "something" is, it's
difficult for anyone to understand what part of the code you're
asking about. And if nothing else, it helps others provide feedback
as to whether the code example was written by someone that you
should
feel comfortable using as a guide for learning C#.


The result of subtracting 10 - 'Z' is -80. Not that that was the
question you asked the first time, but that's the answer.


What's an "ASCII number"? The phrase seems oxymoronic to me.

Not really--if a character is promoted to an int it must be by some
mapping of characters to integers--ASCII is one such mapping, Unicode
is another, EBCDIC is a third, and there are others.
 
Fred said:
I found the following code in an online tutorial, and I'm having some
difficulty understanding it. Unfortunately, it did not list any expected
output examples for val.

What type is val? Is this statement comparing, adding, and subtracting ASCII
numbers?

if ('0' <= ch && ch <= '9') val = ch - '0';
else if ('A' <= ch && ch <= 'Z') val = 10 + ch - 'A';
else {val = 0; Console.WriteLine("invalid character {0}", ch);}

It is all being done as integer arithmetic.

Characters has numeric values.

You can look them up at:
http://en.wikipedia.org/wiki/Ascii#ASCII_printable_characters

if ch is '3' then:

val = ch - '0' = 51 - 48 = 3

if ch is 'B' then:

val = 10 + ch - 'A' = 10 + 66 - 65 = 11

Arne
 
J. Clarke said:
Not really--if a character is promoted to an int it must be by some
mapping of characters to integers--ASCII is one such mapping, Unicode
is another, EBCDIC is a third, and there are others.

It is worth nothing that ASCII is a subset of Unicode not
a completely different mapping.

For the characters relevant for the original question there
are no difference.

Arne
 
Arne said:
It is worth nothing that ASCII is a subset of Unicode not
a completely different mapping.

For the characters relevant for the original question there
are no difference.

Which doesn't make "ASCII number" an oxymoron.
 
Arne Vajhøj said:
It is all being done as integer arithmetic.

Characters has numeric values.

You can look them up at:
http://en.wikipedia.org/wiki/Ascii#ASCII_printable_characters

if ch is '3' then:

val = ch - '0' = 51 - 48 = 3

if ch is 'B' then:

val = 10 + ch - 'A' = 10 + 66 - 65 = 11

Thanks, I got it.

I just didn't understand the notion of promoting characters to ASCII or
Unicode implicitly. Isn't this a great programming language!
 
Hi Fred,

Glad to see the community's reply answers your question.

Just for your information, this type of ASCII character<-->numeric
representation is not the feature of C#. It is the universal standard ANSI
for all programming languages, including C/C++, VB etc...

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Back
Top