PERL Unpack

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

PERL Unpack

Post by SFSxOI »

PERL has an unpack command that unpacks packed strings. Is there a way to implement that in PureBasic?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: PERL Unpack

Post by IdeasVacuum »

Certainly looks possible: http://www.perl.me.uk/perldocs/functions/pack.html

Does it bring about any real advantage though?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: PERL Unpack

Post 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.
Last edited by SFSxOI on Thu Mar 29, 2012 5:34 pm, edited 2 times in total.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: PERL Unpack

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply