Newbie Question on Char Conversion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I would like to know if the following

int cr = 13
sw.Write(Convert.ToChar(cr));
int lf = 10
sw.Write(Convert.ToChar(lf));

Will give me the equivalent of a "Cariage Return" and a "New Line" in ASCII
I know that the ToChar function uses Unicode so that's why I want to know if it is equivalent

Thanks

Jim
 
Yes. The following prints out "they are the same." When the stuff gets
marshalled over to ASCII (from unicode) the upper byte is stripped from each
char, and since the lower bytes are the ones that hold 13 and 10, you have
no loss of your newline.

using System;

namespace TestingGrounds
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(10);
sb.Append(System.Convert.ToChar(13));
sb.Append(System.Convert.ToChar(10));

if(System.Environment.NewLine.CompareTo(sb.ToString())==0)
Console.WriteLine("they are the same.");

return;
}

}
}
 
In addition to Jim's comments, I would suggest using Environment.NewLine
instead (it's portable).
- Pete
 
Back
Top