Converting a char to an int

i have a user input for a command, which is run as a string, i want to give the player an item when they type the /give command and everything works perfect accept i need to be able to convert a char into a string to read it properly
for instance when the player runs the /give command it would look like this
/give 1 10
1 being the item id, and 10 being the quantity
but there is an issue, my inventory manager takes inputs as ints. and clearly this isnt an int
when i try the Convert.toInt32(Line); it gives me a large number 49 for 1 and 32 for 10* i suspect this is the numerical value for the character* is there any way to convert the character “1” into the int 1?

Converting it directly to an int gets the ascii value of the char or something. I believe you can ‘hack’ this by just subtracting the 0 char from it, like this.

int number = Line - '0';

There’s also stuff like GetNumericValue, which I think is the official way.

If your format is always /give 1 10, why not split the string on the space char and then simply convert the “1” into an int with int.parse?

Or, I should say, if it’s always in a predictable pattern of command item id quantity

2 Likes

That is true, I suppose I looked at the question too objectively. I would also suggest doing this, as I’m pretty sure Command Line Arguments are split into strings as well (or char[ ]). It’s also safe to assume this is much better, as with the previous method you could only have 10 item IDs.

Here’s the documentation on the Split method.

Here’s a link to Parse and TryParse. It would be good practice to either use TryParse or put Parse in a try catch block, as you should expect your users to be dumb and put in something that’s NaN.

2 Likes

thanks i had no idea what parse was till just now

ill definitely give these a look