471,310 Members | 1,380 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Character Operators

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);}

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
Sep 5 '08 #1
11 1793
On Fri, 05 Sep 2008 10:56:02 -0700, Fred Chateau
<fc******@community.nospamwrote:
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.
If the online tutorial is relevant to the question (and it seems that it
probably is), you should share the link to the tutorial to provide better
context for your question.
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's not possible to answer "what type is val". That depends on its
declaration, which you didn't show.

As far as the what the code does, it's converting hexadecimal digits to
integers, by taking advantage of the fact that the digit characters '0'
through '9' are contiguous in value as chars, as are the letters 'A'
through 'F'.

The code has at least one problem: it's not limiting input from 'A' to
'F', so unless the intent was to support output of individual digit values
for a base 36 number, it will incorrectly accept certain kinds of invalid
input.

There also is nothing shown in the code you posted to handle lower-case
digits, which would normally be allowable in a typical hexadecimal parsing
situation. However, it's possible there's code elsewhere that you didn't
post that does this, or maybe the problem space disallows lower-case
character input, so it's harder to say whether that's an issue o rnot.

Pete
Sep 5 '08 #2
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.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Fri, 05 Sep 2008 10:56:02 -0700, Fred Chateau
<fc******@community.nospamwrote:
>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.

If the online tutorial is relevant to the question (and it seems that it
probably is), you should share the link to the tutorial to provide better
context for your question.
>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's not possible to answer "what type is val". That depends on its
declaration, which you didn't show.

As far as the what the code does, it's converting hexadecimal digits to
integers, by taking advantage of the fact that the digit characters '0'
through '9' are contiguous in value as chars, as are the letters 'A'
through 'F'.

The code has at least one problem: it's not limiting input from 'A' to
'F', so unless the intent was to support output of individual digit values
for a base 36 number, it will incorrectly accept certain kinds of invalid
input.

There also is nothing shown in the code you posted to handle lower-case
digits, which would normally be allowable in a typical hexadecimal parsing
situation. However, it's possible there's code elsewhere that you didn't
post that does this, or maybe the problem space disallows lower-case
character input, so it's harder to say whether that's an issue o rnot.

Pete

Sep 5 '08 #3
Fred Chateau wrote:
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
Sep 5 '08 #4
On Fri, 05 Sep 2008 12:58:29 -0700, Fred Chateau
<fc******@community.nospamwrote:
That was the entire example, showing a simple example of a For statement.
The example didn't show the declaration of "val"? Doesn't sound like much
of an example to me then.
The context is not relevant to the question I am asking,
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#.
which is, what is
the result of subtracting 10 - 'Z'?
The result of subtracting 10 - 'Z' is -80. Not that that was the question
you asked the first time, but that's the answer.
In other words, are the single quotes
here some sort of shorthand for converting characters to ASCII numbers?
What's an "ASCII number"? The phrase seems oxymoronic to me.

As far as what single quotes do, the single quotes do the same thing here
as they do anywhere in C#: they indicate a character literal.

The subtraction, on the other hand, _does_ wind up converting char types
to int, in a way. To perform the subtraction, the chars are promoted to
ints and so the difference between two chars is an int.
I do not see how this has anything to do with hexadecimal (or any other
base) numbers.
The code you posted wouldn't be used except in converting characters to
numerics. It has everything to do with the numerical base being used, and
hexadecimal is the only commonly used numerical base that includes letters
as digits.

Pete
Sep 5 '08 #5
Peter Duniho wrote:
On Fri, 05 Sep 2008 12:58:29 -0700, Fred Chateau
<fc******@community.nospamwrote:
>That was the entire example, showing a simple example of a For
statement.

The example didn't show the declaration of "val"? Doesn't sound
like
much of an example to me then.
>The context is not relevant to the question I am asking,

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#.
> which is, what is
the result of subtracting 10 - 'Z'?

The result of subtracting 10 - 'Z' is -80. Not that that was the
question you asked the first time, but that's the answer.
>In other words, are the single quotes
here some sort of shorthand for converting characters to ASCII
numbers?

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.
As far as what single quotes do, the single quotes do the same thing
here as they do anywhere in C#: they indicate a character literal.

The subtraction, on the other hand, _does_ wind up converting char
types to int, in a way. To perform the subtraction, the chars are
promoted to ints and so the difference between two chars is an int.
>I do not see how this has anything to do with hexadecimal (or any
other base) numbers.

The code you posted wouldn't be used except in converting characters
to numerics. It has everything to do with the numerical base being
used, and hexadecimal is the only commonly used numerical base that
includes letters as digits.

Pete
--
--
--John
to email, dial "usenet" and validate
(was jclarke at eye bee em dot net)
Sep 5 '08 #6
Fred Chateau wrote:
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#A...ble_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

Sep 6 '08 #7
J. Clarke wrote:
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

Sep 6 '08 #8
Arne Vajhøj wrote:
J. Clarke wrote:
>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.
Which doesn't make "ASCII number" an oxymoron.

--
--
--John
to email, dial "usenet" and validate
(was jclarke at eye bee em dot net)
Sep 6 '08 #9
"Arne Vajhj" <ar**@vajhoej.dkwrote ...
It is all being done as integer arithmetic.

Characters has numeric values.

You can look them up at:
http://en.wikipedia.org/wiki/Ascii#A...ble_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!

--
Regards,

Fred Chateau
fchateauAtComcastDotNet
Sep 8 '08 #10
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:
ms****@microsoft.com.

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

Sep 9 '08 #11
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:
ms****@microsoft.com.

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

Sep 9 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by greg | last post: by
7 posts views Thread by Harlan Messinger | last post: by
2 posts views Thread by Steve Summit | last post: by
16 posts views Thread by thenightfly | last post: by
4 posts views Thread by Brian O'Haire | last post: by
13 posts views Thread by Ivan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.