C# .NET - Ascii to character conversion

Asked By Sharmilee Karunanidhi on 24-Mar-07 12:48 AM
I have converted a character to hexa decimal and the same to ascii value. I want to convert ascii value to corresponding character. Can anyone help me. Its using C#.net




Here's the method

S Sason replied to Sharmilee Karunanidhi on 24-Mar-07 01:32 AM

To convert an ASCII value to corresponding character use the following code

char c;

c=Convert.ToChar(97);  // where 97 is the ascii

Now c will have the character value

  

it can be done using System.Text Namespace

manish bafna replied to Sharmilee Karunanidhi on 24-Mar-07 02:00 AM

Hi,

There are two steps for converting ASCII into string

[1]
int byteCount=System.Text.ASCIIEncoding.ASCII.GetByteCount(urASCIIString);
Byte[] ASCIIByteArray = new Byte[byteCount];

byte[] ASCIIByteArray = System.Text.ASCIIEncoding.ASCII.GetBytes(urASCIIString);

[2] If you want back string
string constructedStringfromascii = System.Text.ASCIIEncoding.GetChars(ASCIIByteArray );

[3] However if you want back characters

char[] chararray;

chararray = constructedStringfromascii .ToCharArray();

Console.Write("Decoded chars: ");

foreach (Char c in chararray ) {
            Console.Write("[{0}]", c);
        }

Hope this helps.

Thanks and Regards,

Manish Bafna.

MCP and MCTS

refer to System.Text.ASCIIEncoding Class

sundar k replied to Sharmilee Karunanidhi on 24-Mar-07 03:29 AM
end of post