C
C#5mo ago
Ewan

✅ How can i convert a character to an in?

Im making a program which takes in a string of numbers and adds each individual number there so like 0123 would make 6 as 0+1+2+3=6 but its not working, help!
No description
28 Replies
Ewan
Ewan5mo ago
it says it cant convert char to an int using parse, why?
Moods
Moods5mo ago
No overload takes a char I assume If you want to use Parse, you can convert the char to a string then use Parse Easiest way Do the char minus - '0'
cap5lut
cap5lut5mo ago
if u know that its a digit u can just do myString[i] - '0'
Ewan
Ewan5mo ago
oh thats genius OH
Moods
Moods5mo ago
It allocates
Ewan
Ewan5mo ago
i see
Moods
Moods5mo ago
Yeah that's better
Ewan
Ewan5mo ago
thank yous!! :DD
cap5lut
cap5lut5mo ago
chars are in the end just like 2 byte numbers and '0' through '9' are sequential, thats why this works
Moods
Moods5mo ago
The reason why my string[i] - '0' works is because internally char are represented as integers, and subtracting one char from another would give the difference in their number representation. And it just so happens that the letter's number representation are in order 👍
Ewan
Ewan5mo ago
oooo ok gotcha thanks a bunch!
Ewan
Ewan5mo ago
No description
Ewan
Ewan5mo ago
uhh @Moods @cap5lut its not working ;-;
Moods
Moods5mo ago
It's already an int
Ewan
Ewan5mo ago
it says i cant convert char to string neither
Moods
Moods5mo ago
When you subtract
Ewan
Ewan5mo ago
AH
Ewan
Ewan5mo ago
No description
Ewan
Ewan5mo ago
ok so when i do this it gives 303 not 15 like i want it to
Moods
Moods5mo ago
0 in single quotes
Ewan
Ewan5mo ago
whats the difference between sinlge and double quotes?
Moods
Moods5mo ago
Single char double string
Ewan
Ewan5mo ago
that worked!! could u explain a bit more please?
Moods
Moods5mo ago
You use single quotes (' ') for char, which are just one char, like 'g' or ':' or '4' And double for string like "hello world!" "Ggggggg" etc
Ewan
Ewan5mo ago
ooo i see ok thank yous so catlaugh
Ewan
Ewan5mo ago
ill have a look at thesee thanks!! catshy