home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!utcsri!torn!nott!bnrgate!bcars6a8!bcars68a!jsparkes
- From: jsparkes@bcars68a.bnr.ca (Jeff Sparkes)
- Subject: Re: unpack and shift idiom?
- Message-ID: <1992Dec18.185143.17096@bcars6a8.bnr.ca>
- Sender: usenet@bcars6a8.bnr.ca (Use Net)
- Nntp-Posting-Host: bcars68a
- Organization: Bell-Northern Research Ltd., Ottawa
- References: <1992Dec16.180058.15351@bcars6a8.bnr.ca>
- Date: Fri, 18 Dec 1992 18:51:43 GMT
- Lines: 39
-
- In article <1992Dec16.180058.15351@bcars6a8.bnr.ca> jsparkes@bcars68a.bnr.ca (Jeff Sparkes) writes:
- >I'm reading binary data from a network stream. I've accumulated the data in
- >a variable called $data, and am taking it apart in chunks. The code
- >looks like:
- > @output = unpack("L4S6", $data);
- > $data = substr($data, 28);
- > @names = unpack("A20", $data);
- > ???
- >
- >I'm concerned by the need to use a constant as the arg to substr, especially
- >since I calculated it wrong the first time, and it took me a while to find.
- >This gets especially hairy when I pull strings out of the data stream, since
- >I don't know how many bytes to skip after unpacking A strings because some
- >stripping occurs.
- >
- >Please tell me I'm missing something!
-
- Well, one thing that I'm missing is that unpack works with fixed size
- structures, and expects strings to be fixed length. (I thought the number
- was how many strings to read, not the legnth of the string. I should learn
- to read documenation properly!) Anyway, I was dealing with structures like:
- struct junk {
- long one, two;
- short three, four;
- char *s1;
- char *s2;
- }
- which it seems perl it not well suited for.
-
- One wish though: it would be great if unpack would actually remove the
- bytes from the front of the string, or at least provide some way to find
- out how many bytes were used in the unpacking.
-
- Oh well, live and learn.
-
- --
- --
- Jeff Sparkes jsparkes@bnr.ca Bell-Northern Research, Ottawa (613)765-2503
- You know, a muffin can be *very* filling.
-