SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: unpack question

  1. #1
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unpack question

    I am new to working with packed data. I have a index file from a client and it is a mixture of packed data and plain string data. I have been able to work out everything except one bit of it.

    There is an bit of data packed as an integer. It goes in as "001" but it comes out as "1". The thing is it can be anything from 1-999, and I will not know ahead of time when the application is in production.

    I know that I could pad the string after it is extracted with zeros to assure it is in the right length, but I wanted to ask to see if I am missing something that I haven't learned yet about the pack and unpack functions of PHP - something that might take care of this issue more reliably (I guess I would really like to get out eactly what is put in without any further manipulation).

    Thanks for the assistance,
    Jeff

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Australia
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to validate the data before it is unpacked - possibly with a regular expression to ensure the input data is of the right length.

    eg \d{3}

    If you want the data to come out as 001..999 then you might want to unpack as a string which will give you the 001 then when you want to use it in a numeric context then cast it as an int (int) $data

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    Uk
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Packing stores it as binary therefore 1 & 001 will be packed as the same value. If the number of preceeding 0's is important and you can modify the format, you could store it as a string.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •