January 15, 2021, 06:04:01 PM

Author Topic: [bb] String - Int - String by Banshee [ 1+ years ago ]  (Read 398 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] String - Int - String by Banshee [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : String - Int - String
Author : Banshee
Posted : 1+ years ago

Description : This simple code converts an integer number to a 4 byte ASCII string which is useful for transmitting numbers in a compressed format in multiplayer applications, rather than sending numbers in the usual ASCII format (myInteger=myString$) which could result in up to 11 bytes of data - plus a 2 byte key for the length...

The =intToString() function returns to the global variable st$ for use.  The =stringToInt() return it's result directly.

Usage example:

print stringToInt(st$)

This isn't the most wonderful or complex code in the world but I needed it for myself and thought i'd share.

Code :
Code: BlitzBasic
  1. Removed: Improved Version Below

Comments :

Banshee(Posted 1+ years ago)

 I found a better way of doing it when I came to solve the problem of floats.usage:floatToString(<float>)print st$stringToFloat(<string>)print rFloat
Code: [Select]
global st$,rFloat#

Function floatToString(value#)
PokeFloat floatBank,0,value
st$=Chr$(PeekByte(floatBank,0)) + Chr$(PeekByte(floatBank,1)) + Chr$(PeekByte(floatBank,2)) + Chr$(PeekByte(floatBank,3))
FreeBank floatBank
End Function
Function stringToFloat(st$)
PokeByte floatBank,0,Asc(Mid(st,1))
PokeByte floatBank,1,Asc(Mid(st,2))
PokeByte floatBank,2,Asc(Mid(st,3))
PokeByte floatBank,3,Asc(Mid(st,4))
FreeBank floatBank
End Function
Function intToString(value)
PokeInt intBank,0,value
st$=Chr$(PeekByte(intBank,0)) + Chr$(PeekByte(intBank,1)) + Chr$(PeekByte(intBank,2)) + Chr$(PeekByte(intBank,3))
FreeBank intBank
End Function
Function stringToInt(st$)
PokeByte intBank,0,Asc(Mid(st,1))
PokeByte intBank,1,Asc(Mid(st,2))
PokeByte intBank,2,Asc(Mid(st,3))
PokeByte intBank,3,Asc(Mid(st,4))
FreeBank intBank
Return value
End Function


SimplePortal 2.3.6 © 2008-2014, SimplePortal