convert text into ascii then from ascii to text

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1573 Views - Last Post: 07 March 2012 - 02:43 AM Rate Topic: -----

#1 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

convert text into ascii then from ascii to text

Posted 03 March 2012 - 04:18 AM

Hello,

I am trying to convert text into ascii I successfully make that by the following code
String input =  "Hello World!";
char[] values =  input.ToCharArray();
byte[] asciitext = new byte[300];
asciitext = Encoding.ASCII.GetBytes(values);
foreach (byte element in asciitext)
        {
            TextBox2.Text += element;
        }



How I can return the ascii to a text???
any help?

Is This A Good Question/Topic? 0
  • +

Replies To: convert text into ascii then from ascii to text

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 04:22 AM

moving to C#
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 04:52 AM

You can turn a byte array into a string using Encoding.GetString or into a char array using Encoding.GetChars.

Some comments on the code you have: You can call GetBytes directly with a string, no need to turn it into a char array first (unless you need the char array for something else anyway). Also I don't know why you did new byte[300], but it doesn't make any sense. You're creating a 300-byte array that you never use and throw away on the very next line. That just creates unnecessary work for the GC (and makes your code more confusing).
Was This Post Helpful? 0
  • +
  • -

#4 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 07:29 AM

I want to convert from ascii to text can u give me code for this?
Was This Post Helpful? -2
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 07:36 AM

As I said already, you can use the Encoding.GetString method to convert an array of bytes to a string in a given encoding. So for ASCII that would be Encoding.ASCII.GetString.
Was This Post Helpful? 0
  • +
  • -

#6 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 08:21 AM

How should use it? is it like this?
TextBox1.Text = Ecoding.ASCII.GetString("105");??
Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 08:28 AM

The argument to GetString is a byte array, not a string. As I said, GetString converts an array of bytes to a string (if that's not what you want, please clarify the question). You can find further information in the documentation.

This post has been edited by sepp2k: 03 March 2012 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#8 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 08:48 AM

look I need to convert for example 105104 which it is ascii to hi which it is string
are you understand me?
Was This Post Helpful? -2
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 09:07 AM

When you say 105104, do you mean an array containing the values 105 and 104? Because that's what I just told you how to do. If not, I do in fact not understand you.
Was This Post Helpful? 0
  • +
  • -

#10 Shenantics  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 4
  • Joined: 03-March 12

Re: convert text into ascii then from ascii to text

Posted 03 March 2012 - 02:18 PM

you need to do something like

bite[] bitearray=new bite[]{255,100,90};
then use bitearray as your argument for the above example

if your reading from a file read each bite into the array at the propper index using ReadBite();
Was This Post Helpful? -3
  • +
  • -

#11 RexGrammer  Icon User is offline

  • Coding Dynamo
  • member icon

Reputation: 181
  • View blog
  • Posts: 777
  • Joined: 27-October 11

Re: convert text into ascii then from ascii to text

Posted 04 March 2012 - 04:54 AM

Look you can't have that input (105104) you need to sanitize it.

You don't know if 1051 is one letter and 04 the other or 105 is the first and 104 the second (just an example). You need to pass each letter as a separate variable.

Why don't you try out the thing you wrote?

Quote

TextBox1.Text = Ecoding.ASCII.GetString("105");


You could try to split the given string into bytes by converting each 3 characters to a byte, but I don't guarantee that you won't get an out of memory exception if you try to insert a value higher than 255 into a byte variable.

View PostShenantics, on 03 March 2012 - 09:18 PM, said:

you need to do something like

bite[] bitearray=new bite[]{255,100,90};
then use bitearray as your argument for the above example

if your reading from a file read each bite into the array at the propper index using ReadBite();


It's byte not bite.

Definition of the word bite:

Quote

verb (used with object)
1.
to cut, wound, or tear with the teeth: She bit the apple greedily. The lion bit his trainer.
2.
to grip or hold with the teeth: Stop biting your lip!
3.
to sting, as does an insect.
4.
to cause to smart or sting: an icy wind that bit our faces.
5.
to sever with the teeth (often followed by off ): Don't bite your nails. The child bit off a large piece of the candy bar.


Source: Bite
Was This Post Helpful? 0
  • +
  • -

#12 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

Re: convert text into ascii then from ascii to text

Posted 06 March 2012 - 04:51 AM

I mean if I write 105 then I want to convert it to its character D
are you understand me now??
Was This Post Helpful? 0
  • +
  • -

#13 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: convert text into ascii then from ascii to text

Posted 06 March 2012 - 06:07 AM

If you want to convert an individual ASCII value to it's corresponding letter, then you can do this...

char letter = (char)105;
textBox1.Text = letter.ToString();



The textbox would show i.

EDITED: fixed typo.
Was This Post Helpful? 0
  • +
  • -

#14 Ayah2012  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 7
  • Joined: 29-February 12

Re: convert text into ascii then from ascii to text

Posted 06 March 2012 - 11:43 PM

the text doesnt work

i mean the code doesn't work with me
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2102
  • View blog
  • Posts: 3,207
  • Joined: 21-June 11

Re: convert text into ascii then from ascii to text

Posted 07 March 2012 - 02:42 AM

Then you must be doing something wrong.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2