Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 132,674 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,205 people online right now. Registration is fast and FREE... Join Now!




Converting From ascii to string

 
Reply to this topicStart new topic

Converting From ascii to string

zahugi
post 14 Sep, 2008 - 10:15 PM
Post #1


New D.I.C Head

*
Joined: 3 Nov, 2007
Posts: 1


My Contributions


hello,
I m trying to write small program convering from string to ascii then converting back from ascii to string.
converting from string to ascii is ok, but to convert from ascii to string it gives me the ascii not string.

my code is:
CODE


ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] a = ascii.GetBytes(textBox2.Text);
            string b = Encoding.ASCII.GetString(a,0,a.Length);
            textBox3.Text = textBox3.Text + b.ToString ();
User is offlineProfile CardPM

Go to the top of the page

ruslan40
post 14 Sep, 2008 - 10:50 PM
Post #2


New D.I.C Head

*
Joined: 13 Sep, 2008
Posts: 11



Thanked 2 times
My Contributions


QUOTE(zahugi @ 14 Sep, 2008 - 11:15 PM) *

hello,
I m trying to write small program convering from string to ascii then converting back from ascii to string.
converting from string to ascii is ok, but to convert from ascii to string it gives me the ascii not string.

my code is:
CODE


ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] a = ascii.GetBytes(textBox2.Text);
            string b = Encoding.ASCII.GetString(a,0,a.Length);
            textBox3.Text = textBox3.Text + b.ToString ();



Hi.

I am not sure if I am correct, since I am fairly new to C# as well. But as far as I know, .NET uses Unicode (UTF8) strings.
So you can try doing this something like this

CODE


            byte[] a = new byte[b.Length];
            char[] s = new char[b.Length];

            int i;

            s = b.ToCharArray();

            for (i = 0; i <= b.Length; i++)
            {
                a[i] = (byte)s[i];
            }

            UTF8Encoding.Convert(Encoding.ASCII, Encoding.UTF8, a);
            textBox3.Text = textBox3.Text + a.ToString();



I dont know if this code will work or not, but I hope this helps.

Good luck!
User is offlineProfile CardPM

Go to the top of the page

ragingben
post 7 Oct, 2008 - 05:33 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 30


My Contributions


Hi there, I've just knocked together some examples that should get you going:

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create some examples
            Int32 ASCII = 69;
            String word = "ABC";

            // Cast to Char - display an integer as its ASCII value - 69 = E
            Console.WriteLine((Char)ASCII);
            // or use the convert method
            Console.WriteLine(Convert.ToChar(ASCII));

            // Display string as series of ASCII values
            foreach (Char character in word)
            {
                // Cast to an integer
                Console.WriteLine((Int32)character);
                // or use convert method
                Console.WriteLine(Convert.ToInt32(character));
            }

            Console.ReadKey();

        }
    }
}


Hope that helps!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 06:25AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month