473,231 Members | 1,913 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 1883
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
2
by: Frank | last post by:
Hello, I'm a total newbie on Javascript, and with a lot of help and some copy and paste managed to get things running. On my site: http://home.wanadoo.nl/homepage I use a form from Bravenet....
7
by: Harlan Messinger | last post by:
How far back in their version history did Netscape and Internet Explorer support — and – codes for em and en dashes in text? In ALT attributes? In TITLE tags? I notice that Netscape 4.7 and 6...
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
2
by: Steve Summit | last post by:
-----BEGIN PGP SIGNED MESSAGE----- It's often explained that the reason for some of the imprecision in C's definition is so that C can be implemented on different kinds of machines -- say, those...
16
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
49
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
4
by: Brian O'Haire | last post by:
Hi I am doing a project where I have to use multiple character sets for the input. They would be English, Symbol and possible Greek, These will be used as symbols for processing an algebraic...
13
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.