Character representation

  • Thread starter Thread starter Gas
  • Start date Start date
G

Gas

Hi,

I am wondering how can I representation some of the specical characters in
C#?
(for example, we use vbKeyTab for Tab and vbCrLf for line feed in VB)

Also is there any character to ASCII code and ASCII code to character
built-in function in C#?

thanks

Gas
 
Gas,

Check out the Keys enumeration in the System.Windows.Forms namespace.
It should have all the keys that you need.

Also, you can't always convert a key to an ASCII code. For example,
what character does F6 represent? What exactly are you trying to do?

Hope this helps.
 
Gas said:
Hi,

I am wondering how can I representation some of the specical characters in
C#?
(for example, we use vbKeyTab for Tab and vbCrLf for line feed in VB)

Also is there any character to ASCII code and ASCII code to character
built-in function in C#?

\r = carriage return
\n = linefeed (\r\n = vbCrLf)
\t = tab
\" = double quote

There are a few other esoteric ones, but those are the most common.

As far as char-to-code and vice versa:

// code will be 32 or 0x20
int code = (int) ' ';

// spaceChar will be ' '
char spaceChar = (char) 32;

Please be aware that strings and chars in .NET are Unicode. If you hit
some special unicode characters or other languages and such, you may
get unexpected results. In general, you should try to avoid worring about
what the ASCII code is for a given char because it's not reliable in an
internationalized sense.

-c
 
To convert between chars and ASCII code, use typecasting. (char)65 gives you
'A'; (int)'A' gives 65.

For keys, see if System.Windows.Forms.Keys enumeration could help you.

Hi,

I am wondering how can I representation some of the specical characters in
C#?
(for example, we use vbKeyTab for Tab and vbCrLf for line feed in VB)

Also is there any character to ASCII code and ASCII code to character
built-in function in C#?

thanks

Gas
 
Hi,

The representation for some of the special characters in c# are as follows,
\t for Tab
\r for Carriage Return
\n for new line

Also, for converting character to ASCII and vise versa...

try using Convert.ToInt32(char) and Convert.ToChar(int) respectively...

Hope this helps...

Regards,
Madhu

MVP-C# | MCSD.NET
 
Also is there any character to ASCII code and ASCII code to character
built-in function in C#?


One of the things that programmers coming from Basic language variant to
a C language variant have trouble with is that Basic imposes a completely
unnecessary distinction between numbers & characters, which C (and C++ and
C#) does not. So, the following code will print " A, 66":

public class MyClass
{
public static void Main()
{
Char c =(Char) 65;
int i = 'B';
Console.WriteLine("{0}, {1}", c, i);
}
}

Note that the cast it required to convert 65 to a character, but not to
convert 'B' into an int.
I am wondering how can I representation some of the special characters in
C#?
(for example, we use vbKeyTab for Tab and vbCrLf for line feed in VB)

So, with the above in mind, you can create Tab as a character simply by:

char csKeyTab = (char) 9;

However, you'll probably want them as string (and for vbCrLf, it would
have to be), inwhich case you can use the predefined "escape codes":

string csCrLf = "\r\n";
string csKeyTab = "\t";

But say you want a string containing a special character which doesn't
have a predefined code? This can be done also, but you'll have to translate
the ASCII code into Hexadecimal.

string csABC = "\x41\x42\x43"; // "ABC" "A" = ASCII 65 (dec) = 41
(hex)

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
 
James Curran said:
But say you want a string containing a special character which doesn't
have a predefined code? This can be done also, but you'll have to translate
the ASCII code into Hexadecimal.

string csABC = "\x41\x42\x43"; // "ABC" "A" = ASCII 65 (dec) = 41
(hex)

I would recommend using \uxxxx format rather than \x, as that way you
don't need to worry about where the representation finished. For
instance,

\x12dog and \x12cat will have different first letters - the first will
have unicode 0x012d and the second will have unicode 0x12ca (I
believe). Using

\u0012dog and \u0012cat the compiler knows what to do.
 
Just wondering do I really need the casting in C# here?
because I know in C++,
Char c= 65; will work

Gas
 
Gas said:
Just wondering do I really need the casting in C# here?
because I know in C++,
Char c= 65; will work

C/C++ have a very bad habit of confusing integers with text. There's no
theoretical reason why C# couldn't make the cast implicit, but the
language designers chose not to.
 
Back
Top