6 Replies - 653 Views - Last Post: 03 August 2015 - 12:05 PM Rate Topic: -----

#1 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 572
  • Joined: 19-May 14

How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 06:19 AM

So i am trying to learn C# because i have nothing better to do in my vacation :P
And im trying to convert an letter to an ascii code and use the ascii code as a number to do calculations with it.
I am getting this error:
Additional Information: The layout of the input string is incorrect.

At this string:
int iletter = Int32.Parse(Encoding.ASCII.GetBytes(letter.ToString()).ToString());


What does this error mean? And how can i fix it.

Is This A Good Question/Topic? 0
  • +

Replies To: How can i fix: The layout of the input string is incorrect.

#2 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,724
  • Joined: 12-December 12

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 06:31 AM

Assuming that letter is a string then letter.ToString() is redundant.

GetBytes returns an array of bytes:
string value = "whatever";

// Convert the string into a byte[].
byte[] asciiBytes = Encoding.ASCII.GetBytes(value);

You'll have to join this array if you want to display a string like "11910497..".

But, if letter consists of a single character then this approach is overkill. E.g.
            string letter = "C";
            MessageBox.Show(((int)letter[0]).ToString());

This post has been edited by andrewsw: 03 August 2015 - 06:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 06:31 AM

Long answer that will help you with your learning endeavors: If you break up your code into multiple lines and inspect each of the values, the error will become pretty obvious:
string letterString = letter.ToString();
byte [] byteData = Encoding.ASCII.GetBytes(letterString);
string byteDataString = byteData.ToString();
int iletter = Int32.Parse(byteData);



Short answer: you are going about getting the ASCII code of each character in the string the wrong way. You could simply access each character of the string just like you would in C/C++.
Was This Post Helpful? 1
  • +
  • -

#4 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 572
  • Joined: 19-May 14

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 09:27 AM

Haha yes but i am familiar with vb.net i am kinda used to it now. I was learning C# and i wanted to convert the letter as Char to a string.

I wrote
letter.tostring;

wich was the first mistake i made because a friend told me i forgot the () signs at the end like
letter.ToString();

Also letter is a char
I want to process each ASCII value of a character.

            foreach (char letter in textBox1.Text)
            {

                int iletter = Int32.Parse(Encoding.ASCII.GetBytes(letter.ToString()).ToString());

                Console.Write(finalnumber.ToString());
            }


afcourse the Console.writeline is just to show its working.
Was This Post Helpful? 0
  • +
  • -

#5 RamonRobben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 572
  • Joined: 19-May 14

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 10:01 AM

Yeah got it working by seperating my code. and also the byte is an array because those [] stand for array.
So i made a foreach loop to loop trough each byte in the array and converting it to integer to do calculations with it.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 11:51 AM

Seems pretty circuitous. Assuming you're dealing with ASCII to begin with, all you have to do is cast each char in the string to an int or byte, without jumping through any hoops.

For the record, what kind of "calculations" can you not do with bytes? Bytes are just integers with a much smaller min/max. You can cast them on the fly if you need to. In many cases they can implicitly be treated as integers, since no data loss can occur going from lower precision to higher.

Anyway, I can think of three simple ways to get what you're looking for with far less hoops to jump through. I'll work under the assumption that you have to have ints, though I doubt that is as required as you think it is:

var str = "this is string";

//way 1:
int[] output1 = str.Select(c => (int)c).ToArray();
//way 2:
int[] output2 = Encoding.ASCII.GetBytes(str).Select(x => (int)x).ToArray();
//way 3
List<int> output3l = new List<int>();
foreach(char c in str) { output3l.Add((int)c); }
int[] output3 = output3l.ToArray();


None of these involve the intermediary of making a number into a string back into a number. No reason to do conversion when casting is available. The first two leverage LINQ, but the last does not, if you'd rather program more imperatively (Technically the .ToArray() call is defined in the LINQ libraries. You can just work with the list if that is actually a problem).

Whichever way you choose, now you have all the characters from the string as integers, in an array. You can loop through the array one at a time to get each value and do whatever calculation you need.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,724
  • Joined: 12-December 12

Re: How can i fix: The layout of the input string is incorrect.

Posted 03 August 2015 - 12:05 PM

Quote

You can cast them on the fly if you need to.

An example of this is:
    string alp = "abc";
    int total = 0;
    foreach (char c in alp) {
        total += (int)c;
    }
    MessageBox.Show(total.ToString());

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1