Convert string to int 32 and store your result

4

I am developing a multiplayer game in Unity using the services of Google Play Game Services (GPGS), each of the players that connects gets a player id through GPGS is id is a string which I need to convert to whole and its elements add them to obtain a numerical value of each user and be able to perform a certain action.

The problem is not converting the string to int, it is already converted I can not perform any operation with them.

The code I have is the following:

//Declaración de variables
char[] caracter;
int[] valId;

//Inicialización de variables
caracter = nextParticipantId.ToCharArray();
valId = new int[]{0,0,0,0,0,0,0,0,0,0}

//Sección del código
foreach( char letter in caracter) {
    Debug.Log("La letra es: " + letter);
    Debug.Log("El valor de la letra es: " + Convert.ToInt32(letter));
    valId[i] += Convert.ToInt32(letter);
}

The first Debug shows me the letter without problem, the second shows me its value without any problem too, the conflict is when I try to add the values of those letters.

    
asked by Jorge Sánchez 16.06.2016 в 02:21
source

3 answers

2

You must use Convert.ToInt32 or Int32.Parse but you have to store the result in another variable of integer type to be able to manipulate it.

Example:

var cadena = "1234";
var numero = Convert.ToInt32(cadena);
numero += 1; // ahora es 1235

Keep in mind that the chain is still a chain.

Greetings

    
answered by 16.06.2016 в 03:52
1

Well, I have never used the class Convert but doing tests with its conversion functions, I have found that maybe your problem is as follows:

char Caracter = '3'; // 51 En ASCII ...
int Valor = Convert.ToInt32(Caracter); // Valor actual: 51

So when you try to convert a character using Convert you are taking its value in the ASCII character table and not the real number, I recommend you do any of the following actions:

Taking the following character as an example:

char c = '3'; // 51

Change the function by int.Parse(string) :

int V = int.Parse(c.ToString()); 

Or cast a string directly in the function Convert.ToInt32(object) :

int V = Convert.ToInt32(c.ToString());

If you want to get the current value of the character and not the value in the ASCII table.

Edit:

Applying the solution to your current problem, just change this line:

valId[i] += Convert.ToInt32(letter);

By:

valId[i] += Convert.ToInt32(letter.ToString());

And, looking a bit more your code, where does the index of the array come from? As you can see, you use the foreach loop, are you sure you do not assign all values to the same index?

I hope it has helped you!

    
answered by 16.06.2016 в 04:52
0

The problem is that you do not modify the index of the array you are using:

//Declaración de variables
char[] caracter;
int[] valId;

//Inicialización de variables
caracter = nextParticipantId.ToCharArray();
valId = new int[]{0,0,0,0,0,0,0,0,0,0}

//Sección del código
int i = 0;
foreach( char letter in caracter) {
    Debug.Log("La letra es: " + letter);
    int letra = Convert.ToInt32(letter);
    Debug.Log("El valor de la letra es: " + letra);
    valId[i++] += letra;
}
    
answered by 16.06.2016 в 12:29