473,325 Members | 2,792 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,325 software developers and data experts.

Character representation

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
Nov 16 '05 #1
8 9647
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Gas" <ga****@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #2

"Gas" <ga****@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #3
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.

"Gas" <ga****@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
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

Nov 16 '05 #4
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

"Gas" wrote:
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

Nov 16 '05 #5
"Gas" <ga****@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
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!)
Nov 16 '05 #6
James Curran <Ja*********@mvps.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Gas
Just wondering do I really need the casting in C# here?
because I know in C++,
Char c= 65; will work

Gas

"James Curran" <Ja*********@mvps.org> wrote in message
news:u9**************@TK2MSFTNGP15.phx.gbl...
"Gas" <ga****@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...
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!)

Nov 16 '05 #8
Gas <ga****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ajay | last post by:
hi! i am calculating a digest of some data (using hmac) and printing it to a cookie. the problem is the hash may contain special characters. when these are printed they get converted to...
2
by: Hans Mabelis | last post by:
I'm new here; got here because suddenly the question came up: is html a 7-bit or an 8-bit language? Officially, I mean. I seem to consistently suffer from character set issues. Of course, I can...
76
by: Zenobia | last post by:
How do I display character 151 (long hyphen) in XHTML (utf-8) ? Is there another character that will substitute? The W3C validation parser, http://validator.w3.org, tells me that this character...
50
by: The Bicycling Guitarist | last post by:
A browser conforming to HTML 4.0 is required to recognize &#number; notations. If I use XHTML 1.0 and charset UTF-8 though, does &eacute; have as much support as é ? Sometimes when I run...
37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
40
by: Shmuel (Seymour J.) Metz | last post by:
I'd like to include some Hebrew names in a web page. HTML 4 doesn't appear to include character attributes for ISO-8859-8. I'd prefer avoiding numeric references, e.g.,...
3
by: stormandstress | last post by:
Hi. I'm writing a program that is dependent on the curses library and functions for python, and I'm a little puzzled by the way characters are handled. The basics of the program are that a...
17
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
2
by: George2 | last post by:
Hello everyone, I need to know the wide character (unicode) and multibyte (UTF-8) values of a character string of czech. I personally know nothing about czech. Is the following approach...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.