parseInt('bcedfg',16)
Base on this code I am getting 773855 in a JavaScript,
I go through the conversion table , and not sure how to get this "773855" value
my question is how parseint come out with this "773855" value , because I wanted to translate this tiny code in to a c# code , and is that any similar way in c# allow me to get this "773855" value
The bcedfg
is being interpreted as hex(base: 16
). Yet the hex scale rolls over after f
so, in javascript, the g
and everything after it is being removed before converting. With that said, here's how the conversion works:
decValueOfCharPosition = decValueOfChar * base ^ posFromTheRight
so:
b = 11 * 16^4 = 11 * 65536 = 720896
c = 12 * 16^3 = 12 * 4096 = 49152
d = 13 * 16^2 = 13 * 256 = 3328
e = 14 * 16^1 = 14 * 16 = 224
f = 15 * 16^0 = 15 * 1 = 15
Once you know the decimal value for each character's position, just add them together to get the converted decimal value:
b(720896) + c(49152) + d(3328) + e(224) + f(15) = 773855
C and it's offspring are not so lenient when it comes to the input. Instead of removing the first invalid character and any characters that may follow after, C throws a Format Exception
. To work around this to get the javascript functionality you must first remove the invalid character and it's followings, then you can use:
Convert.ToInt32(input, base)
I didn't include a way to remove invalid characters due to there being multiple ways of doing as such and my subpar knowledge of C
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With