admin管理员组文章数量:1291178
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 e 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
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 e 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
Share Improve this question edited Oct 21, 2012 at 9:55 gdoron 150k59 gold badges302 silver badges371 bronze badges asked Oct 21, 2012 at 9:47 abc cbaabc cba 2,63311 gold badges32 silver badges50 bronze badges 1- The problem here is the "bcedfg" is be interpreted as hex(base: 16). Yet the hex scale rolls over at "f". – SReject Commented Oct 21, 2012 at 9:50
2 Answers
Reset to default 10The 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 es 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
Use Convert.ToInt32
:
Convert.ToInt32(theString, 16)
It uses this overload:
public static int ToInt32(string value, int fromBase)
Note that you will get FormatException
because of the g
in the string:
FormatException
value contains a character that is not a valid digit in the base specified by fromBase. The exception message indicates that there are no digits to convert if the first character in value is invalid; otherwise, the message indicates that value contains invalid trailing characters.
MSDN
You're not getting error in javascript as js tends to "forgive" too much errors, this one one of those examples.
本文标签: cHow parseInt get its valueStack Overflow
版权声明:本文标题:c# - How parseInt get its value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741527677a2383566.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论