13 Replies - 7522 Views - Last Post: 02 February 2011 - 03:36 PM Rate Topic: -----

#1 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

ASCII int array to String?

Posted 01 February 2011 - 10:53 PM

I am trying to create an output message in simple calculator that reiterates the user input then displays the answer.

I am trying to use ASCII codes to create a string like " X ", or " + ".

The code works as is but the comments at lines 45 & 46 are my new train of thought to try and reduce the size of line 48.

Any suggestions on how to concatenate an int array to a string? Is int the best type for this array?

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lesson5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void okbutton_Click(object sender, EventArgs e)
        {   double firstvalue = double.Parse(term1TxtBox.Text);
            double secondvalue = double.Parse(term2TxtBox.Text);
            double result = 0;
            int []operand = new int[3];
            switch (operatorComboBox.Text)
            {
                case "Add":
                    //use ASCII codes in array to create "space/operator/space" stirng sequence
                    operand = new int[] {32,43,32}; 
                    result = firstvalue + secondvalue;    
                    break;
                case "Subtract":
                    operand = new int[] {32,45,32};
                    result = firstvalue - secondvalue;
                    break;
                case "Multiply":
                    operand = new int[] {32,88,32};
                    result = firstvalue * secondvalue;
                    break;
                case "Divide":
                    operand = new int[] {32,247,32};
                    result = firstvalue / secondvalue;
                    break;
            }
            /* create string from ASCII value array
            for (int i = 0; i < 2; ++i)
            {   string asciiString = Convert.ToChar(operand[i]);
            */
                resultLabel.Text = (firstvalue.ToString()) + Convert.ToChar(operand[0]) + Convert.ToChar(operand[1]) + Convert.ToChar(operand[2]) + (secondvalue.ToString()) + " = " + result;
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: ASCII int array to String?

#2 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 08:22 AM

use a byte array first off (you'll see why in a second)

then go and check out the System.Text.ASCIIEncoding class:
http://msdn.microsof...iiencoding.aspx

or more specifically the ASCIIEncoding.GetString(byte[]...) method:
http://msdn.microsof...y/744y86tc.aspx

it converts a byte array to a string treating each byte in the array as an ascii character value.
Was This Post Helpful? 3
  • +
  • -

#3 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

Re: ASCII int array to String?

Posted 02 February 2011 - 01:15 PM

OK, I'm stuck.

I just can't figure out the correct syntax for Encoding.GetString().

Here are some of my attempts.
The operand[] array and all initializations have been changed to type byte.

/*create sting from ASCII value array
                for (int i = 0; i < 2; ++i)
             string asciiString = Encoding.GetString(operand[i]);
            */

            //string asciiString = Encoding.GetString(operand);
            
            /*string asciiString;
            for (int i = 0; i < 2; ++i)
            asciiString = Encoding.GetString(operand[i]);
            */

            resultLabel.Text = (firstvalue.ToString()) + Encoding.GetString(operand[]) + (secondvalue.ToString()) + " = " + result;

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: ASCII int array to String?

Posted 02 February 2011 - 01:37 PM

Encoding.GetString() takes a byte array as an argument. So first, you need to change your array from an int array to a byte array.

Then you would do this...

string asciiString = Encoding.GetString(operand);


Was This Post Helpful? 0
  • +
  • -

#5 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 01:41 PM

View Postlordofduct, on 02 February 2011 - 08:22 AM, said:

use a byte array first off (you'll see why in a second)

...snip...

it converts a byte array to a string treating each byte in the array as an ascii character value.

This post has been edited by lordofduct: 02 February 2011 - 01:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

Re: ASCII int array to String?

Posted 02 February 2011 - 02:08 PM

As stated in my last post, the array has been changed to a byte.

Using:

string asciiString = Encoding.GetString(operand);


Gets an error CS0120: An object reference is required for the non-static field, method, or property 'System.Text.Encoding.GetString(byte[])'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lesson5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            double firstvalue = double.Parse(term1TxtBox.Text);
            double secondvalue = double.Parse(term2TxtBox.Text);
            double result = 0;
            //int[] operand = new int[3];
            byte[] operand = new byte[3];
            switch (operatorComboBox.Text)
            {
                case "Add":
                    //use ASCII codes in array to create "space/operator/space" stirng sequence
                    //operand = new int[] { 32, 43, 32 };
                    operand = new byte[] { 32, 43, 32 }; 
                    result = firstvalue + secondvalue;    
                    break;
                case "Subtract":
                    //operand = new int[] { 32, 45, 32 };
                    operand = new byte[] { 32, 45, 32 };
                    result = firstvalue - secondvalue;
                    break;
                case "Multiply":
                    //operand = new int[] { 32, 88, 32 };
                    operand = new byte[] { 32, 88, 32 };
                    result = firstvalue * secondvalue;
                    break;
                case "Divide":
                    //operand = new int[] { 32, 247, 32 };
                    operand = new byte[] { 32, 247, 32 };
                    result = firstvalue / secondvalue;
                    break;
            }
            //create sting from ASCII value array
            /*
            for (int i = 0; i < 2; ++i)
                string asciiString = Encoding.GetString(operand[i]);

            Convert.ToChar(operand[0]) + Convert.ToChar(operand[1]) + Convert.ToChar(operand[2])
            */

            string asciiString = Encoding.GetString(operand);
            resultLabel.Text = (firstvalue.ToString()) + asciiString + (secondvalue.ToString()) + " = " + result;
        }
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: ASCII int array to String?

Posted 02 February 2011 - 02:17 PM

That's because GetString isn't a static method.

System.Text.Encoding encoding = System.Text.Encoding.UTF8;
string asciiString = encoding.GetString(operand);


Was This Post Helpful? 1
  • +
  • -

#8 [email protected]   User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 02:21 PM

It's

Encoding.ASCII.GetString(operand);


not:

Encoding.GetString(operand);


You missed the encoding property out (ASCII, in the above example. There are other encoding properties like Unicode etc) :).

This post has been edited by [email protected]: 02 February 2011 - 02:23 PM

Was This Post Helpful? 2
  • +
  • -

#9 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 02:30 PM

note in my links I pointed at the ASCIIEncoding class, and the GetString method of it.

You're (OP) using the Encoding class:
http://msdn.microsof...t.encoding.aspx

Which is the abstract class (can't be instantiated) from which all the different Encoding classes (like ASCIIEncoding) extend from.

The Encoding class also happens to have a static interface to quickly grab an instance of a given encoding class quickly.

Thusly the ability to say:

System.Text.Encoding.ASCII.GetString(myArr)



this part:
Encoding.ASCII



is a static getter that hands out an instance of System.Text.ASCIIEncoding

see here:
http://msdn.microsof...ding.ascii.aspx

This post has been edited by lordofduct: 02 February 2011 - 02:31 PM

Was This Post Helpful? 2
  • +
  • -

#10 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

Re: ASCII int array to String?

Posted 02 February 2011 - 02:38 PM

Apparently

System.Text.Encoding.Default.GetString(myArr)


will also work.

Can anyone tell me where I missed this part in the MSDN library explanation?

Encoding.GetString Method (Byte[])
Was This Post Helpful? 0
  • +
  • -

#11 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 02:41 PM

View PostWCoaster, on 02 February 2011 - 02:38 PM, said:

Apparently

System.Text.Encoding.Default.GetString(myArr)


will also work.


That's because of here:
http://msdn.microsof...ng.default.aspx

read:
" Caution
Different computers can use different encodings as the default, and the default encoding can even change on a single computer. Therefore, data streamed from one computer to another or even retrieved at different times on the same computer might be translated incorrectly. In addition, the encoding returned by the Default property uses best-fit fallback to map unsupported characters to characters supported by the code page. For these two reasons, using the default encoding is generally not recommended. To ensure that encoded bytes are decoded properly, your application should use a Unicode encoding, such as UTF8Encoding or UnicodeEncoding, with a preamble. Another option is to use a higher-level protocol to ensure that the same format is used for encoding and decoding.


The system ANSI code page defined by Default covers the ASCII set of characters, but the encoding is different from the encoding for ASCII. Because all Default encodings lose data, your application might use UTF8 instead. UTF-8 is often identical in the U+00 to U+7F range, but can encode other characters without loss."



Quote

Can anyone tell me where I missed this part in the MSDN library explanation?

Encoding.GetString Method (Byte[])


That's because in my post above I had two links...

to the ASCIIEncoding class:
http://msdn.microsof...iiencoding.aspx

and to the GetString method which is a member of ASCIIEncoding, but because it's defined in Encoding the link brought you to Encoding.GetString:
http://msdn.microsof...y/744y86tc.aspx

This post has been edited by lordofduct: 02 February 2011 - 02:46 PM

Was This Post Helpful? 2
  • +
  • -

#12 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

Re: ASCII int array to String?

Posted 02 February 2011 - 03:03 PM

OK I think I may know a little more than yesterday.

Thanks
Everyone

string asciiString = ASCIIEncoding.Default.GetString(operand);


Works also. :eek:

However

string asciiString = ASCIIEncoding.ASCII.GetString(operand);


Causes the division symbol to display as the default '?'

This post has been edited by WCoaster: 02 February 2011 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#13 lordofduct   User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2668
  • View blog
  • Posts: 4,786
  • Joined: 24-September 10

Re: ASCII int array to String?

Posted 02 February 2011 - 03:28 PM

that's because you say under division:

operand = new byte[] { 32, 247, 32 };



on the standard ascii table , not extended ascii, 247 doesn't exist (ascii maxes at 127). On extended ascii (ISO 8859-1) 247 the old school ÷ symbol. Usually in the ascii set people use the / symbold for division (which is ascii char 47).

from the ASCIIEncoding page:
http://msdn.microsof...iiencoding.aspx

"ASCIIEncoding corresponds to the Windows code page 20127. Because ASCII is a 7-bit encoding, ASCII characters are limited to the lowest 128 Unicode characters, from U+0000 to U+007F. If the default encoder returned by the Encoding.ASCII property or the ASCIIEncoding constructor is used, characters outside that range are replaced with a question mark (?) before the encoding operation is performed..."


you can get the ISO 8859-1 encoding like this I believe:

Encoding.GetEncoding("iso 8859-1")



or just use the UTF-8 encoding...

Encoding.UTF8



the table for which can be googled for easily... 247 is the division sign in it...

This post has been edited by lordofduct: 02 February 2011 - 03:36 PM

Was This Post Helpful? 2
  • +
  • -

#14 WCoaster   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-January 11

Re: ASCII int array to String?

Posted 02 February 2011 - 03:36 PM

Food for thought in my search of this topic.

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1