UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on December 08, 2023, 08:41:01 AM

Title: Pack & Unpack String Array To String Functions
Post by: kevin on December 08, 2023, 08:41:01 AM
  Pack & Unpack String Array To String Functions


 The provided PlayBASIC code defines two functions, PackArrayToString and UnPackStringToArray, which are designed to pack a string array into a single string and unpack a packed string back into a string array, respectively. Below is a beginner-friendly summary of the code:

   PackArrayToString Function:

       Purpose: Packs a given string array into a single string.
       Input: Takes a string array (Array$()).
       Output: Returns a packed string (Result$).
       Description:
           Calculates the size of the array and converts it to a hexadecimal string.
           Iterates through each element in the array.
           For each element, converts its length to a hexadecimal string and appends it to the result string.
           Appends the actual string data to the result string.
           Returns the final packed string.

 
[pbcode]
Function PackArrayToString(Array$())
  Size = getarrayelements(Array$())
  Result$ = hex$(Size)

  for lp = 0 to Size    
     S$ = Array$(lp)
     StringLen = Len(S$)
     Result$ += Hex$(StringLen) + S$  
  next  
EndFunction Result$()
[/pbcode]



UnPackStringToArray Function:

   Purpose: Unpacks a packed string into a string array.
   Input: Takes a packed string (ThisSTRING$).
   Output: Returns a string array (Result$()).
   Description:
       Extracts the size of the array from the first part of the packed string.
       Initializes an array to store the unpacked strings.
       Iterates through each element in the packed string.
       Extracts the length of the string and the actual string data.
       Appends the extracted string to the result array.
       Returns the final string array.

[pbcode]
Function UnPackStringToArray(ThisSTRING$)
      Size = Val(Mid$(ThisSTRING$, 1, 9))
      Dim Result$(Size)  

      CurrentPOS = 10  
      for lp = 0 to Size    
         ThisCHR = Mid(ThisSTRING$, CurrentPOS)
         s$ = Mid$(ThisSTRING$, CurrentPOS, 9)
         StringLEN = Val(s$)
         CurrentPOS += 9
         Result$(lp) = Mid$(ThisSTRING$, CurrentPOS, StringLEN)
         CurrentPOS += StringLEN
      next  
EndFunction Result$()
[/pbcode]

The provided example demonstrates the usage of these functions by creating a string array, packing it into a single string, unpacking it back into an array, and then comparing the original and unpacked arrays. The results are printed to the debug console.




[pbcode]




   Size=100
   Dim Stuff$(Size)

   // String Array with some junk strings
   for lp =0 to size
      ThisChr=rndrange(asc("a"),asc("z"))
      Stuff$(lp) = Make$(chr$(ThisCHR),lp+1)
   next


   //  Pack all the data from the array into a single string
   result$= PackArrayToString(Stuff$())


   //  Render the result out the debug console
   #print Result$


   //  Create an empty array
   Dim Output$(0)


   // Unpack the encoded string back into an array
   Output$()=UnPackStringToArray(Result$)


   //  Pack this array and compare with the original.
   result2$= PackArrayToString(Output$())

   // Render this to the debug console
   #print Result2$
   print "Result$=Result2$


   print "Done"
   Sync
   waitkey


Function PackArrayToString(Array$())
   
   Size=getarrayelements(Array$())
   Result$=hex$(Size)
   
   for lp=0 to Size    
         S$=Array$(lp)
         StringLen=Len(S$)
         Result$+=Hex$(StringLen)+s$   
    next   
   
ENdFunction Result$



Function UnPackStringToArray(ThisSTRING$)

   Size = Val(Mid$(ThisSTRING$,1,9))
   Dim Result$(Size)   
   
   CurrentPOS = 10   
   for lp=0 to Size    
      ThisCHR=mid(ThisSTRING$,CurrentPOS)

      s$=Mid$(ThisSTRING$,CurrentPOS,9)
      StringLEN = Val(s$)

      CurrentPOS+=9
      REsult$(lp) = Mid$(ThisSTRING$,CurrentPOS,StringLEN)
      
      CurrentPOS+= StringLEN
   next   
   
ENdFunction Result$()



[/pbcode]


  - PasteBin (https://pastebin.com/Xf114hZd) version of Source Code