Add your own alternative version
Stats
28.8K views 781 downloads 15 bookmarked
Posted
10 Sep 2013
|
Comments and Discussions
|
|
Doesn't work for negative numbers
|
|
|
|
|
ale44ko
|
|
|
|
|
"Convert" isn't a very descriptive name.
"digits.Substring(CharIdx, 1)"
No need for Substring, just use CharIdx as an index.
",+,/."
Using mathematical operators as digits can be confusing; I use _ and $ .
You can eliminate the ubase variable by iterating left-to-right and multiplying by radix directly.
Some times, as when using HEX, the input string contains lower-case rather than upper-case, you should consider handling that gracefully.
|
|
|
|
|
Thank you for your comments. Points taken.
1) I don't like it either. Do you have any suggestions? ToBaseString? ToRadixString?, EncodeToBase? ToString? Encode? (perhaps last two are better names)
2) I'll replace substring with index. good catch.
3) The last two characters in digits string: "+/" are there because I took last two characters from commonly used set digits used for Base-64 encoding. But that relevant only for numbers written as base-63 and higher. Also keep in mind that "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,+,/" is default set of digit and you can replace it with anything in your own implementation.
5) Agree - will do code improvement.
6) I was thinking about it. If there is no confusion (no upper/lower chars dups in relevant subset of digits) I'm going to accept both lower and upper characters for conversion. This will be in updated version.
Again thanks a lot for yur contrbution.
|
|
|
|
|
|
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
|