How To Separate a 2 digit int into single values: Help

Hi there,
If I have a int of 23… how can I separate the value, 2 and the value 3? Keep in mind, the value in question could be any two digit number.

Any ideas?

Something like this?

// Psuedo Code

int someTwoDigitNum = 34;
List<int> digits = new List<int>();

string num = someTwoDigitNum.ToString();
for(int i; i < num.Length; i++)
{
    digits.Add(int.Parse(num[i]));
}

Edit:
See Dave-Carlile 's response. Much, much better than this garbage.

1 Like

Thanks

But I am getting an error, in the console. Apparently I am not using the best overload method in the arguments.

My situation is that I am gathering the values from a 2D array. My code looks like this…

        //now, we must get the order of the move pattern...
        for(int x = 0; x < synched_move_pattern.rows.Length; x++){
            for(int z = 0; z < synched_move_pattern.rows[x].row.Length; z++){
                int value = synched_move_pattern.rows[x].row[z];
                List<int> digits = new List<int>();
                string num = value.ToString();
                for(int i; i < num.Length; i++){
                    digits.Add(int.Parse(num[i]));
                    print ("digit broken down " + int.Parse(num[i]));
                }
            }
        }

Error on line 9:
Assets/Scripts/Enemies/AI/PatternMng/Pattern_Prime.cs(90,21): error CS1502: The best overloaded method match for `int.Parse(string)’ has some invalid arguments

int value = 23;
int digit1 = value / 10;      // integer divide by 10 will give you the 10s place
int digit2 = value % 10;   // modulus 10 gives the remainder after division, which is 3
6 Likes

Thanks…
Perhaps this is appropriate to ask this second question here…

The reason to am doing this is to gain a list of directions (1,2,3,4)… but to insure the direction are in correct order of the path I am creating, I am using 11, 22, 33, 44…

So in this case, the first digit will mark the order of the path position while the second the direction to take once there.

My issue is this… I can gather and split the ints properly now, but my order is off… I can sort the order of the order_list I gathered, but how do I corsspond it to the order of the directions list?

Hi there,
I have created this method, to gather the value of each individual character in a string of numbers, converted from an int.

it is a 5 digit value, that I am entering, and each character, has a significance. My issue, is that, my results are non single digit numbers that do not reflect the initial value at all???
What am I doing incorrectly?

For example, when I enter, 11111, as my 5 digit value, each of the characters as value, which I retrieve from this, should be, 1. However, each is 49!!!??

WHATTTT? lol. Please help.

    /// <summary>
    /// USING A 5 DIGIT VALUE IN THE PATH LAYOUT
    /// </summary>
    /// <param name="x">The x coordinate.</param>
    /// <param name="z">The z coordinate.</param>
    void spawn_layout_as(int x, int z){

        //////-------
        int unitValue = spawn_layout.rows[x].row[z];

        if (unitValue == 0) {
            return;
        } else {

            print("unit value " + unitValue);
            ///create node from pool.
            Pattern_Node node = pool_patternNodes [0];
            pool_patternNodes.Remove (node);
            pattern_nodes.Add (node);


            // get various values to store in node.
            //kind of creature
            int unitType = 0;
            // do we spawn here? 1 = yes, 0 = no. (as this may be a path point, but not a spawn point.
            int spawnPoint = 0;
            //rank of node, thus apply to init move count
            int index = 0;
            //rtn
            int rtn = 0;
            //dir of path at this node
            int dir = 0;
            //pos of this node.
            Vector3 pos = new Vector3 ((int)x, 1, (int)z);

            string num = unitValue.ToString();
            for(int i = 0; i < num.Length; i++){
                if (i == 0) {
                    unitType = num [i];
                    print ("UnitType = " + unitType);
                }
                if (i == 1) {
                    spawnPoint = num [i];
                    print ("spawnPoint = " + spawnPoint);
                }
                if (i == 2) {
                    index = num [i];
                    print ("index = " + index);
                }
                if (i == 3) {
                    rtn = num [i];
                    print ("rtn = " + rtn);
                }
                else if (i == 4){
                    dir = num [i];
                    print ("dir = " + dir);
                }
            }

              
            ///at each instance of node, we have a unit, which will store the values of order in path, dir in path and position in path
            node.set_values (unitType, index, dir, rtn, pos, this);
  

            ///at this point, if spawnIdex == 1, we can spawn unitType at pos.
            if (spawnPoint == 1) {
                Pattern_Unit_Creator.ins.create_unit (node);
            }

        }
    }

Thanks lol, much better than my dirty, dirty way. I didnt think of that way. That should come in handy. +1

2 Likes

The indexing operator of string returns a character which you’re coercing into an int when you assign it to unitType. So you’re getting the integer value of the character ‘1’.

Honestly - this all sounds ridiculous. What are you even trying to do?

Hi,
I have a system in which I want to be able to…
create a path.
mark positions of the path
mark the direction to turn at said position
mark if the position requires a spawn
mark the index of the position

Right now, I am doing this via a 2D array (as int) (10x10 grid). So, any value that I enter, must be 5 digits, where [0] is creature type, [1] do I actually spawn, [2] index of the position in path, [3] direction to turn at position, [4] rotation to set at.

??

PS. So how do I get “1” to return as (int) 1

but what if I have a 5 digit number? ex 12345

Where each value must be its own, single int value so 1,2,3,4,5?

The fundamental issue is the compilation of the 2D array, order. And that, my path order will not (chances are) follow this route.

Cheat, and subtract 48 from the char value
int0 = someString[0] - 48

not that this system completely limits you to only 10 of anything (10 creature types, path of length 10 etc)

Nice One!!!
That works brilliantly.
and 10 is fine, as it is 10x10.

Why does the char return 49 tho? is it just 1 as char is 49?

Anyhow,
Thanks!!

10x10 = 100
you never have a path that bends around and travels more?

Why not create a class that contains these values as booleans. Then just store instances of the class in your array. No need to do all this funky parsing.

1 Like

This.

Don’t fabricate nonsensical data structures that require you to write a bunch of unreadable boilerplate code just to derive actual meaning.

1 Like

I have.
My issue is in the sorting of the 2D array.

A 2D array compiles the values within, moving left to right, down, left to right, down and so on.

So, in my case if you imagine a 2D array in your inspector… Now imagine this 10x10 array has 4 values filled, which make a closed path 1, 2, 3, 4, so a virtual square pattern, where 1, is top left (it is compiled first), 2 is top right, ( it is compiled second), 3 is bottom right (remember the path, as such, it is compiled 4th), while 4 is compiled 3rd.

So, where I am stuck is in the gathering of the order of the array. And then readjusting any aascocited lists order.

At the moment, I have a container, with the index (as determined by me), along with other variables. My trouble is that I want to create a List where the order of this new List, is reflective of the index values (which are in random order).

Hope this makes sense.
Cheers

Make a 2D array of your data class which contains information about the point at that X,Y coordinate not a 2D array of magic integers that mean arbitrary things.

Heh. Well, it’s not too far off from being able to support an arbitrary sized int.

Here’s how I’d do it:

       public static List<int> IntToList(int value)
       {
           List<int> list = new List<int>();

           do
           {
               list.Add(value % 10);
               value /= 10;
           }
           while (value > 0);

           return list;
       }
3 Likes

Ok, so here is an image of the 2D array. You can see its layout. The path moves clockwise, from top left 1 to bottom right 6.

In the pic, you see the compilation order, gets tangled when i = 4 (“4 as 6”).

So here is what I need help with, and a 1000 hi fived if you can correct my ignorance.

I need to be able to resort, or apply the order to a new list. So that it goes from 1,2,3,4,6,5, to 1,2,3,4,5,6.

You see what I mean?
Thank you