Code archives/Algorithms/Integer to Base 36
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
These two functions can be used to convert between decimal and base 36. This is useful for packing/unpacking large integers. | |||||
Function int_to_base36$(value) conv$="" While value remainder=value Mod 36 value=value/36 If remainder<10 conv$=conv$+Chr$(48+remainder) Else conv$=conv$+Chr$(55+remainder) EndIf Wend tmp$="" For x=0 To Len(conv$) tmp$=tmp$+Mid$(conv$,Len(conv$)-x,1) Next conv$=tmp$ If conv$<>"" Return conv$ Else Return "0" EndIf End Function Function base36_to_int(value$) conv=0 For x=1 To Len(value$) ascii=Asc(Mid$(value$,x,1)) If ascii>57 ascii=ascii-7 EndIf ascii=ascii-48 conv=conv+(ascii)*36^(Len(value$)-x) Next Return conv End Function |
Comments
| ||
That's funny. I did this last year in a backup app I wrote for work. I never even considered that someone else might find it useful. :) |
| ||
I was tinkering around with trying to make a packer similar to Python's struct module, and these were the most crucial functions. I believe it would be useful for save game files and networked games. Struct actually breaks your data down into a binary format though. Seeing as the Blitz network commands already do this (I believe) I figured this would be another option. The packer would simply compile a string of characters and then parse them according to the given format of data types. Then you could send the string using Write/Read String. I suspect an increase in speed as opposed to using multiple WriteShort/WriteByte because you're only sending a single string through the stream. |
Code Archives Forum