| 09-18-2007, 02:37 PM | #1 |
Can someone teach me, or tell me how to convert from base A to base N without first going through base 10? I made the following script, but the problem is that I convert the base A to base 10, stick it in an integer, and then convert it to base N. Now, it works good and all, but what happens when the number is too big in base 10 and exceeds the integer's limit. I have to be able to avoid using the integer. JASS:struct Number integer size = 0 integer base = 0 integer array digit [1000] method convertBase takes integer outputBase returns nothing local integer i = 1 local integer n local integer val = 0 local integer pow = 1 loop exitwhen i > .size set val = val + pow*.digit[i] // try to avoid all this set pow = pow * .base set i = i + 1 endloop set i = 1 loop set n = val - (val / outputBase) * outputBase if n < 0 then set n = n + outputBase endif set .digit[i] = n set val = val / outputBase exitwhen val <= 0 set i = i + 1 endloop set .size = i set .base = outputBase endmethod method setNumber takes integer val returns nothing local integer i = 1 local integer n loop set n = val - (val / 10) * 10 if n < 0 then set n = n + 10 endif set .digit[i] = n set val = val / 10 exitwhen val <= 0 set i = i + 1 endloop set .size = i set .base = 10 endmethod method displayNum takes nothing returns string local string charMap = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" local integer maxBase = StringLength(charMap) local integer i = .size local string s = "" if .base > maxBase then return "Base > Max Base" endif loop exitwhen i < 1 set s = s+SubString(charMap, .digit[i], .digit[i]+1) set i = i-1 endloop return s endmethod endstruct |
| 09-18-2007, 03:24 PM | #2 |
JASS:function toStringBaseN takes integer n, integer b returns string local integer d local string s = "" local string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" //check for special cases and validate input if (b < 2 or b > 36) then return "Unsupported Base" elseif (n == 0) then return "0" elseif (n < 0) then return "-" + toStringBaseN(-n) endif //get digits by repeated division/remainder loop exitwhen n == 0 set d = ModuloInteger(n, b) set s = SubStringBJ(chars, d, d) + s set n = n / b endloop return s endfunction ... or something like that |
| 09-18-2007, 10:20 PM | #3 |
That just converts a number in base 10 to base N. I want to go from base A to Base N (which I already have) but skipping base 10. |
| 09-18-2007, 11:08 PM | #4 |
It converts a number in base 2^31 (or therebouts) to base b. I think 2^31 is what you mean by base 10. If I'm understanding you correctly than you need to bite the bullet and use a big num library of some sort. There's one in my save system. |
| 09-19-2007, 12:32 AM | #5 |
Damn... oh well. I want to do the coding mostly on my own, cause I love to learn new math concepts. This is one of the reasons I don't plan to use your save system. Now I just have to figure out how BigNums works, or a method I can use to increase the integer bit size. |
| 09-19-2007, 04:48 AM | #6 |
Professors love to teach this to CS undergrads. You should have no problem finding material via google on implementing arbitrary length integers. |
| 09-19-2007, 06:21 AM | #7 |
Sweet. So the thing has a name! I'll start reading up on it tomorrow. Thanks for all your help. |
