Code archives/Algorithms/String - Int - String
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
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: myVariable=-2147483648 intToString(myVariable) 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. | |||||
Removed: Improved Version Below |
Comments
| ||
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 global st$,rFloat# Function floatToString(value#) floatBank=CreateBank(4) 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$) floatBank=CreateBank(4) 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)) rFloat#=PeekFloat(floatBank,0) FreeBank floatBank End Function Function intToString(value) intBank=CreateBank(4) 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$) intBank=CreateBank(4) 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)) value=PeekInt(intBank,0) FreeBank intBank Return value End Function |
Code Archives Forum