Str_to_Int not working in BMax, why?

BlitzMax Forums/BlitzMax Programming/Str_to_Int not working in BMax, why?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

After doing a replace of all OR and AND to | and & it worked.

by Koriolis
at http://koriolis.free.fr/forum/viewtopic.php?t=53

Function test(n%) 
 s$ = IntToVarLenStr(n)    
 n2% = VarLenStrToInt(s)    
 DebugLog n + " -> " + n2 + " [" + Len(s) + " byte(s)]" 
End Function 



test(0) 
test(-1) 
test(1) 
test(63) 
test(64) 
test(-64) 
test(-65) 
test(8191) 
test(8192) 
test(-8192) 
test(-8193) 
test(1048575) 
test(1048576) 
test(-1048576) 
test(-1048577) 
DebugLog ("--- random ---") 


For i = 1 To 15 
	n% = Rand(0,$10000) | (Rand(0,$10000) Shl 16)
	test(n) 
Next 


Function IntToVarLenStr$(n%) 
 If n < 0   
   n = ((~n) Shl 1) | 1 
 Else 
   n = n Shl 1 
 EndIf 
  
 If Not (n & $ffffff80)   
 	Return Chr(n | $80) 
 ElseIf Not (n & $ffffc000)    
 	Return Chr(n Shr 7) + Chr((n & $7f) | $80) 
 ElseIf Not (n & $ffe00000)    
 	Return Chr(n Shr 14) + Chr((n Shr 7) & $7f) + Chr((n & $7f) | $80) 
 ElseIf Not (n & $f0000000)    
	 Return Chr(n Shr 21) + Chr((n Shr 14) & $7f) + Chr((n Shr 7) & $7f) + Chr((n & $7f) | $80) 
 Else 
 	Return Chr(n Shr 28) + Chr((n Shr 21) & $7f) + Chr((n Shr 14) & $7f) + Chr((n Shr 7) & $7f) + Chr((n & $7f) | $80) 
 EndIf 
End Function 


Function VarLenStrToInt%(s$) 
 Local n% = 0 
 Local b% = Asc(s)  
 s = Right(s, Len(s)-1) 
 If b < $80    
 n = b 
 b = Asc(s)  
 s = Right(s, Len(s)-1) 
 If b < $80    
   n = (n Shl 7) | b 
   b = Asc(s)  
   s = Right(s, Len(s)-1) 
   If b < $80    
   n = (n Shl 7) | b 
   b = Asc(s)  
   s = Right(s, Len(s)-1) 
   If b < $80    
   n = (n Shl 7) | b 
   n = (n Shl 7) | (Asc(s) & $7f) 
   Else 
   n = (n Shl 7) | (b & $7f) 
   EndIf 
   Else 
   n = (n Shl 7) | (b & $7f) 
   EndIf 
 Else 
   n = (n Shl 7) | (b & $7f) 
 EndIf 
 Else 
 n = b & $7f 
 EndIf 
  
 If (n & 1)    
 Return ~(n Shr 1) 
 Else 
 Return n Shr 1 
 EndIf 
End Function


Koriolis I have to say I'm Impressed! The code is public right?

I can't say I can follow the code, but I'm quite sure I can use it =)

I do have one problem with it..

Let's say someone want to send their X,Y,Dir in a package then I'll put them all into string form and then add them together PlayerData$ =IntToVarLenStr$(X)+IntToVarLenStr$(Y)+IntToVarLenStr$(Dir). Now the receiveing player have to know the size of each X,Y,Dir somehow to be able to decode it into a usable int form.

Example: if I'm at position 12,123 Dir 90 then I could send my fullupdate as a 3byte package! Impressive! But how would the receiveing player know that my X,Y,Dir are 1byte each? It seems like I have to send something like this:

PlayerData$ = IntToVarLenStr$(X)+"/"+IntToVarLenStr$(Y)+"/"+IntToVarLenStr$(Dir)

So that I can sort the values from eachother. Am I right? Do I still win data this way?

Because it is a very very convinient way to do it, but it adds uneccecery data. Perhaps some bytes +/- doesn't matter in the end.. I don't know.

Great job with the functions anyway!