January 26, 2021, 12:52:48 PM

Author Topic: [bb] Integer to Base 36 by Sauer [ 1+ years ago ]  (Read 515 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Integer to Base 36 by Sauer [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Integer to Base 36
Author : Sauer
Posted : 1+ years ago

Description : These two functions can be used to convert between decimal and base 36.  This is useful for packing/unpacking large integers.

Code :
Code: BlitzBasic
  1. Function int_to_base36$(value)
  2.         conv$=""
  3.         While value
  4.                 remainder=value Mod 36
  5.                 value=value/36
  6.                 If remainder<10
  7.                         conv$=conv$+Chr$(48+remainder)
  8.                 Else
  9.                         conv$=conv$+Chr$(55+remainder)
  10.                 EndIf
  11.         Wend
  12.        
  13.         tmp$=""
  14.         For x=0 To Len(conv$)
  15.                 tmp$=tmp$+Mid$(conv$,Len(conv$)-x,1)
  16.         Next
  17.         conv$=tmp$
  18.  
  19.         If conv$<>""
  20.                 Return conv$
  21.         Else
  22.                 Return "0"
  23.         EndIf
  24. End Function
  25.  
  26. Function base36_to_int(value$)
  27.         conv=0
  28.         For x=1 To Len(value$)
  29.                 ascii=Asc(Mid$(value$,x,1))
  30.                 If ascii>57
  31.                         ascii=ascii-7
  32.                 EndIf
  33.                 ascii=ascii-48
  34.                 conv=conv+(ascii)*36^(Len(value$)-x)
  35.         Next
  36.         Return conv
  37. End Function


Comments :


TaskMaster(Posted 1+ years ago)

 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.  :)


Sauer(Posted 1+ years ago)

 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.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal