Page 1 of 1

PERL Unpack

Posted: Wed Mar 28, 2012 7:27 pm
by SFSxOI
PERL has an unpack command that unpacks packed strings. Is there a way to implement that in PureBasic?

Re: PERL Unpack

Posted: Thu Mar 29, 2012 3:04 am
by IdeasVacuum
Certainly looks possible: http://www.perl.me.uk/perldocs/functions/pack.html

Does it bring about any real advantage though?

Re: PERL Unpack

Posted: Thu Mar 29, 2012 5:14 pm
by SFSxOI
IdeasVacuum , thanks for pointing out that link.

After looking at that link, and then some simlar questions on the 'net for implementing a pack/unpack in other languages, It looks like the pack side comes down to simply putting things in a binary string, and the unpack comes down to extracting that binary string into readable text, so....maybe this?...

Code: Select all

; the "pack"
Procedure.s BinaryToText(bin_in$)
  Protected out_txt_bin$, Ch$, unkvar.i
   out_txt_bin$=""
   For countx = 1 To Len(bin_in$) / 8
      Ch$ = Mid(bin_in$, 8*(countx-1)+1 ,8)
      unkvar=0
      For countz = 1 To 8
         Select Mid(Ch$, countz,1)
            Case "0" : unkvar = 2 * unkvar
            Case "1" : unkvar = 2 * unkvar+1
         EndSelect
      Next countz
      out_txt_bin$ = out_txt_bin$ + Chr(unkvar)
   Next countx
   ProcedureReturn out_txt_bin$
 EndProcedure

; the 'un-pack'
Procedure.s CharToBin(chrbin.s)
  Protected outbin.s, chr_asc.i, TotCharAsc.i
  outbin = ""
  chr_asc = 0
  TotCharAsc = 0
  chr_asc = Asc(chrbin)
  For countz = 7 To 0 Step -1
    If chr_asc >= TotCharAsc + Pow(2, countz) 
      TotCharAsc = TotCharAsc + Pow(2, countz)
      outbin = outbin + "1"
    Else
      outbin = outbin + "0"
    EndIf
  Next
  ProcedureReturn outbin
EndProcedure

Procedure.s TextToBinary(str2bin.s)
  Protected binout.s
  binout = ""
  For countx = 1 To Len(str2bin)
    binout = binout + CharToBin(Mid(str2bin, countx, 1))
  Next
  ProcedureReturn binout
EndProcedure

xx$ = "Be sure to drink your milk."
Debug "Our text we will convert > " + xx$
; convert the text to binary - 'pack' ????
bin$ = TextToBinary(xx$)
; the binary
Debug "The binary > " + bin$ ; our binary string - 'pack' ??
; converting binary back to text and reading our original text to verify - 'un-pack' ????
Debug "Our original text convert from binary back to text > " + BinaryToText(bin$)

Of course there is probably more to it. But something like this to put stuff in a binary string but I don't know if this is really what they mean by pack and unpack. I don't know what advantages if any are had from a pack/unpack, I recently ran into what was referenced as being "packed strings" and had not encountered this before. Knowing that there are people around here on the forum that are familiar with PERL (which I know nothing about really) and seeing it used as a function in PERL coding, just thought i'd ask so I could explore this a little more.

Re: PERL Unpack

Posted: Thu Mar 29, 2012 5:29 pm
by IdeasVacuum
I think you have the gist of it, nice work, but I'm not sure the effort achieves something that I would find useful, hence my question.