Welcome to Dream.In.Code
Become a C# Expert!

Join 150,199 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,989 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
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
+Quote Post

ruslan40
RE: Converting From Ascii To String
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
+Quote Post

ragingben
RE: Converting From Ascii To String
7 Oct, 2008 - 05:33 AM
Post #3

D.I.C Head
Group Icon

Joined: 7 Oct, 2008
Posts: 60



Thanked: 2 times
Dream Kudos: 100
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
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:55AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month