✅ 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!
28 Replies
it says it cant convert char to an int using parse, why?
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'
if u know that its a digit u can just do
myString[i] - '0'
oh thats genius
OH
It allocates
i see
Yeah that's better
thank yous!! :DD
char
s are in the end just like 2 byte numbers and '0'
through '9'
are sequential, thats why this worksThe 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 👍
oooo ok gotcha thanks a bunch!
uhh
@Moods @cap5lut
its not working ;-;
It's already an int
it says i cant convert char to string neither
When you subtract
AH
ok so when i do this
it gives 303
not 15 like i want it to
0 in single quotes
whats the difference between sinlge and double quotes?
Single char double string
that worked!!
could u explain a bit more please?
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
ooo i see ok thank yous so
ill have a look at thesee thanks!!