C
C#11mo 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
EwanOP11mo ago
it says it cant convert char to an int using parse, why?
Moods
Moods11mo 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
cap5lut11mo ago
if u know that its a digit u can just do myString[i] - '0'
Ewan
EwanOP11mo ago
oh thats genius OH
Moods
Moods11mo ago
It allocates
Ewan
EwanOP11mo ago
i see
Moods
Moods11mo ago
Yeah that's better
Ewan
EwanOP11mo ago
thank yous!! :DD
cap5lut
cap5lut11mo ago
chars are in the end just like 2 byte numbers and '0' through '9' are sequential, thats why this works
Moods
Moods11mo 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
EwanOP11mo ago
oooo ok gotcha thanks a bunch!
Ewan
EwanOP11mo ago
No description
Ewan
EwanOP11mo ago
uhh @Moods @cap5lut its not working ;-;
Moods
Moods11mo ago
It's already an int
Ewan
EwanOP11mo ago
it says i cant convert char to string neither
Moods
Moods11mo ago
When you subtract
Ewan
EwanOP11mo ago
AH
Ewan
EwanOP11mo ago
No description
Ewan
EwanOP11mo ago
ok so when i do this it gives 303 not 15 like i want it to
Moods
Moods11mo ago
0 in single quotes
Ewan
EwanOP11mo ago
whats the difference between sinlge and double quotes?
Moods
Moods11mo ago
Single char double string
Ewan
EwanOP11mo ago
that worked!! could u explain a bit more please?
Moods
Moods11mo 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
EwanOP11mo ago
ooo i see ok thank yous so catlaugh
Ewan
EwanOP11mo ago
ill have a look at thesee thanks!! catshy
Want results from more Discord servers?
Add your server