Newbie question on string operators in C#


R

Richard

Can anyone help a VB programmer understand the following C# code snippet
please? This is excerpted from a base conversion function that converts s (a
string representing a number in any base 2 to 36) into a string representing
the number in another base.

if (s >= 'A' && s <= 'Z') { fs[k++] = 10 + (int)(s - 'A'); }

si is simply a character in a string passed to the function.

fs, according to the author, is an "array of integer digits representing the
number in base:from".

What I don't understand is .... (int) (s - 'A') , since both s and A
are characters.

If, say, s evaluates to 'F', then how does C# treat (int) ('F' - 'A')?
Does it simply convert the two characters into their ASCII numbers, thereby
evaluating it to 5?

TIA
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Richard said:
Can anyone help a VB programmer understand the following C# code snippet
please? This is excerpted from a base conversion function that converts s (a
string representing a number in any base 2 to 36) into a string representing
the number in another base.

if (s >= 'A' && s <= 'Z') { fs[k++] = 10 + (int)(s - 'A'); }

si is simply a character in a string passed to the function.

fs, according to the author, is an "array of integer digits representing the
number in base:from".

What I don't understand is .... (int) (s - 'A') , since both s and A
are characters.

If, say, s evaluates to 'F', then how does C# treat (int) ('F' - 'A')?
Does it simply convert the two characters into their ASCII numbers, thereby
evaluating it to 5?


Pretty much - except using Unicode, not just ASCII. In fact, the (int)
part is unnecessary, as there's no operator to subtract one char from
another - both are promoted to int first, then the subtraction occurs
with an int result.
 
P

Peter Morris

If, say, s evaluates to 'F', then how does C# treat (int) ('F' - 'A')?
Does it simply convert the two characters into their ASCII numbers,
thereby evaluating it to 5?

It would really only take a few seconds to do something like this

char a = 'A';
char f = 'F';

console.WriteLine("F - A = " + (f - a));


Pete
 
D

David Anton

In VB, that would be:
If s(i) >= "A"c AndAlso s(i) <= "Z"c Then
fs(k) = 10 + CInt(Fix(s(i) - AscW("A"c)))
k += 1
End If

Unlike VB, in C# 'character math' is possible (but not very common). In C#
characters have this dual nature - what your intuition says characters are in
addition to the underlying numeric value.

--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to VB & C#
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
Ad

Advertisements

D

David Anton

(correction - missed an 'AscW' - the following will now compile)
If s(i) >= "A"c AndAlso s(i) <= "Z"c Then
fs(k) = 10 + CInt(Fix(AscW(s(i)) - AscW("A"c)))
k += 1
End If
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to VB & C#
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI


David Anton said:
In VB, that would be:
If s(i) >= "A"c AndAlso s(i) <= "Z"c Then
fs(k) = 10 + CInt(Fix(s(i) - AscW("A"c)))
k += 1
End If

Unlike VB, in C# 'character math' is possible (but not very common). In C#
characters have this dual nature - what your intuition says characters are in
addition to the underlying numeric value.

--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
Java to VB & C#
Instant C#: convert VB to C#
Instant VB: convert C# to VB
Instant C++: VB, C#, or Java to C++/CLI


Richard said:
Can anyone help a VB programmer understand the following C# code snippet
please? This is excerpted from a base conversion function that converts s (a
string representing a number in any base 2 to 36) into a string representing
the number in another base.

if (s >= 'A' && s <= 'Z') { fs[k++] = 10 + (int)(s - 'A'); }

si is simply a character in a string passed to the function.

fs, according to the author, is an "array of integer digits representing the
number in base:from".

What I don't understand is .... (int) (s - 'A') , since both s and A
are characters.

If, say, s evaluates to 'F', then how does C# treat (int) ('F' - 'A')?
Does it simply convert the two characters into their ASCII numbers, thereby
evaluating it to 5?

TIA
 

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

Top