Share via


Converting Char Array to Int.

Question

Saturday, September 5, 2015 6:53 PM

I have several variables to convert a char array to int, I want the integer to have the individual char at one time rather than adding the chars together,

for (int i = 0; i <= FirstNumberLength; i++)
                        {
                            FirstTempString = FirstDigits[i].ToString();
                            FirstTemp = Int32.Parse(FirstTempString);
                            SecondTempString = SecondDigits[i].ToString();
                            SecondTemp = Int32.Parse(SecondTempString);

All replies (14)

Monday, September 7, 2015 1:17 AM ✅Answered

OK, I have a working solution for you! Here it is:

char[] answer;
string answerString;
int answerInt;
if (FirstNumberLength >= SecondNumberLength)
    answer = new char[FirstNumberLength];
else
    answer = new char[SecondNumberLength];

int temp;
for (int i = 0; i < answer.Length; i++)
{
    temp = 0;
    if (i < FirstNumberLength)
        temp += int.Parse(FirstDigits[i].ToString());
    if (i < SecondNumberLength)
        temp += int.Parse(SecondDigits[i].ToString());

    answer[i] = (temp.ToString().ToCharArray())[0];
}
answerString = string.Concat(answer);
answerInt = int.Parse(answerString);

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Monday, September 7, 2015 2:20 PM ✅Answered

Thanks for your reply, however the error that has appeared suggests that the int.Parse returns the value as a DateTime object as oppose to an int.

What's the error? This worked just fine for me, given the example input you mentioned in your previous post. FirstNumberString = "123" and SecondNumberString = "45". It gave a result of answerString "861" and answerInt 861.

I used a bit of "set up" code, just prior to the code I posted above:

string FirstNumberString = "123";
string SecondNumberString = "45";
char[] FirstDigits = FirstNumberString.ToCharArray();
char[] SecondDigits = SecondNumberString.ToCharArray();
int SecondNumberLength = SecondDigits.Length;
int FirstNumberLength = FirstDigits.Length;
Array.Reverse(FirstDigits);
Array.Reverse(SecondDigits)

Maybe you should post the code you tried to use, and also tell us the error message you get.

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Saturday, September 5, 2015 7:00 PM

Hi TomB1995,

You can look into below article. 

1. Help converting Char array to Int array

http://forums.asp.net/t/1155991.aspx?Help+converting+Char+array+to+Int+array

2. How can I convert a string to an int array in C#?

https://www.quora.com/How-can-I-convert-a-string-to-an-int-array-in-C

Thanks,

Sabah Shariq


Saturday, September 5, 2015 7:39 PM

An array of digits can be converted using this technique:

char[] digits = { '1', '9', '9', '5' };

int number = int.Parse( string.Concat( digits ) );

// result: 1995.

If this does not work, then show more details about your arrays.


Sunday, September 6, 2015 3:15 PM

Thanks for your replies, however, I don't want the array of the numbers or a total of the numbers, just each individual number until it loops again. I think I copied in too little code in my question. 

char[] FirstDigits = FirstNumberString.ToCharArray();
                    char[] SecondDigits = SecondNumberString.ToCharArray();
                    SecondNumberLength = SecondDigits.Length;
                    FirstNumberLength = FirstDigits.Length;
                    Array.Reverse(FirstDigits);
                    Array.Reverse(SecondDigits);
                    if (FirstNumberLength > SecondNumberLength)
                    {
                        for (int i = 0; i <= FirstNumberLength; i++)
                        {
                            FirstTempString = FirstDigits[i].ToString();
                            FirstTemp = Int32.Parse(FirstTempString);
                            SecondTempString = SecondDigits[i].ToString();
                            SecondTemp = Int32.Parse(SecondTempString);

The FirstNumberString is found using the code below...

string EnteringNumbers(int Number)
        {
            if (First)
            {
                FirstNumber = FirstNumber * 10 + Number;
                FirstNumberString = FirstNumber.ToString();
                return FirstNumberString;
            }

And this line is how Number is found... 

FirstNumberString = EnteringNumbers(1);


Sunday, September 6, 2015 4:41 PM | 1 vote

Hi Tom,

I think you need to show more code and also explain better what it is you're trying to accomplish. I'm still confused looking at your latest code (unless maybe I just need more coffee this morning).

What are you doing with FirstTemp and SecondTemp? You didn't show the rest of your for loop.

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Sunday, September 6, 2015 6:05 PM

I didn't show the reset of the loop as its a lot of repeating in several if statements, right first a button will have this code to get the individual number:

FirstNumberString = EnteringNumbers(1);
txt_current.Text = FirstNumberString;

Below is the function that has been called:

string EnteringNumbers(int Number)
        {
            if (First)
            {
                FirstNumber = FirstNumber * 10 + Number;
                FirstNumberString = FirstNumber.ToString();
                return FirstNumberString;
            }
         }

Then to work this all out another button has the following code:

char[] FirstDigits = FirstNumberString.ToCharArray();
                    char[] SecondDigits = SecondNumberString.ToCharArray();
                    SecondNumberLength = SecondDigits.Length;
                    FirstNumberLength = FirstDigits.Length;
                    Array.Reverse(FirstDigits);
                    Array.Reverse(SecondDigits);
                    if (FirstNumberLength > SecondNumberLength)
                    {
                        for (int i = 0; i <= FirstNumberLength; i++)
                        {
                            FirstTempString = FirstDigits[i].ToString();
                            FirstTemp = Int32.Parse(FirstTempString);
                            SecondTempString = SecondDigits[i].ToString();
                            SecondTemp = Int32.Parse(SecondTempString);
                            if (SecondTemp == 1 || SecondTemp == 0)
                            {
                                if (Temp > 0)
                                {
                                    NumberHolder = FirstTemp + SecondTemp + Temp;
                                    Temp = 0;
                                }
                                else
                                {
                                    NumberHolder = FirstTemp + SecondTemp;
                                }
                                if (NumberHolder == 0)
                                {
                                    answerTemp = 0;
                                }

The rest of the for loop and if statement is repeated with different values but mostly the same. And just for record below are the variables and their types.

int function;
bool First;
int FirstNumber;
string FirstNumberString;
int SecondNumber;
string SecondNumberString;
int Temp;
int FirstTemp;
string FirstTempString;
int SecondTemp;
string SecondTempString;
int one;
int zero;
int FirstNumberLength;
int SecondNumberLength;
int NumberHolder;
int answer;
int answerTemp;

I'm trying to convert the char array to an int so I can later add them but not to values in the same array.


Sunday, September 6, 2015 8:07 PM

A few more questions, Tom, sorry:

  1. Give us an example of the input and output. IOW, show us some sample data for FirstNumberString and SecondNumberString and then what the resulting integers should be.
  2. What is Temp and where and what does it get set to? You show that it gets re-set to 0 in the for loop if it is > 0, but where does it get set to other values? I think we need to know that, since you're adding it to get a value of NumberHolder.
  3. Last question ... what variable is supposed to hold the results? I see NumberHolder as a possibility. But what about answerTemp ... I notice that you're showing answer as a variable in your list of variables, but I don't see it in the code. Is answer supposed to hold the results? Where does that fit in with answerTemp?

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Sunday, September 6, 2015 8:34 PM

The code below is all the times that answer/answerTemp and Temp get called,

if (Temp > 0)
                                {
                                    NumberHolder = FirstTemp + SecondTemp + Temp;
                                    Temp = 0;
                                }
                                else
                                {
                                    NumberHolder = FirstTemp + SecondTemp;
                                }
                                if (NumberHolder == 0)
                                {
                                    answerTemp = 0;
                                }
                                if (NumberHolder == 1)
                                {
                                    answerTemp = 1;
                                }
                                if (NumberHolder == 2)
                                {
                                    answerTemp = 0;
                                    Temp = 1;
                                }
                                if (NumberHolder == 3)
                                {
                                    answerTemp = 1;
                                    Temp = 1;
                                }
                                answer = answer * 10 + answerTemp;

I haven't currently got the answer going anywhere yet. I'm waiting for the code to complete first then add the output later, the only values that can be added to Number, therefore all First variants and Second variants is 1 and 0 since it all goes into the EnteringNumbers function, shown earlier. 


Sunday, September 6, 2015 9:04 PM

Sorry if I sound frustrated, Tom ... but I still don't know what you're trying to accomplish. Knowing that might make it easier to suggest code that will work for you.

That said, you've got to have some idea of what kind of output you're expecting for certain kinds of input. Otherwise, how can you write the code to do it? Just an example, such as this input: "123" and "456", should produce this output: ??? ... what would the ??? be?

I can't look at the code you posted and try to figure it out, because obviously if your code was working, you wouldn't be asking for help.

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com


Sunday, September 6, 2015 9:18 PM

Okay, say the first number in a char array was 123 and the second number in a separate array was 45 how would I go about doing it so that 3 + 5 then output into a variable then 2 + 4 added to the same variable then put 1 into the same variable so the answer was 861. I've got the first bit done by reversing the array.


Monday, September 7, 2015 12:35 PM

Thanks for your reply, however the error that has appeared suggests that the int.Parse returns the value as a DateTime object as oppose to an int.


Tuesday, September 8, 2015 12:38 PM

Thank you. I've managed to fix this using your code, I think I was over-complicating the problem. Once I understood your code I was able to delete about 100 lines of code.


Tuesday, September 8, 2015 6:06 PM | 1 vote

Yes, you were overcomplicating things, definitely!  ;) ;)  I'm sorry, I assumed you'd understand that the code I posted would basically replace all the complicated stuff you had.  I'm glad that you figured that out. Sorry for the misunderstanding!

~~Bonnie DeWitt [C# MVP]

http://geek-goddess-bonnie.blogspot.com