RE: Binary number manipulation

Discussion in 'Python' started by SBrunning@trisystems.co.uk, Dec 1, 2003.

  1. Guest

    > From: Matthew A. Berglund [SMTP:]
    > I am very new to python so I am working on some ass-umptions which may be
    > incorrect (please forgive me).
    >
    > I am working on a problem that requires me to take 2 decimal integers,
    > each of which represents half of a word. I need to slap these things
    > together and get out another decimal integer.
    > A couple of questions:
    > 1. Is there a way to typecast a variable as a binary?


    AFAIK, no.

    > 2. If 1 is no, does that mean that I need to do all the manipulation in
    > some icky string format and then go back?


    I use:

    ((high << 16) | low)

    > I do see the bit-wise operations available and this looks like I need to
    > simply perform these on my integers? Can it really be that simple?


    Yup.

    Cheers,
    Simon Brunning,
    http://www.brunningonline.net/simon/blog/
    --LongSig




    -----------------------------------------------------------------------
    The information in this email is confidential and may be legally privileged.
    It is intended solely for the addressee. Access to this email by anyone else
    is unauthorised. If you are not the intended recipient, any disclosure,
    copying, distribution, or any action taken or omitted to be taken in
    reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
    accept liability for statements made which are clearly the senders own.
    , Dec 1, 2003
    #1
    1. Advertising

  2. Tim Roberts Guest

    wrote:
    >
    >> 2. If 1 is no, does that mean that I need to do all the manipulation in
    >> some icky string format and then go back?

    >
    >I use:
    >
    >((high << 16) | low)


    ....which, unfortunately, gives a FutureWarning in Python 2.3 if high
    happens to be larger than 32767.

    I think we're all going to be using a lot more of the struct module in the
    future. For example, this is the equivalent of what you posted:

    struct.unpack('>L',struct.pack('>2H',high,low))[0]

    I may have to start using things like:

    def MAKELONG(high,low):
    return struct.unpack('>L',struct.pack('>2H',high,low))[0]

    Is there a smarter way to do this kind of thing? This also works:
    ((high << 16L) | low)
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Dec 3, 2003
    #2
    1. Advertising

  3. Georgy Pruss Guest

    "Tim Roberts" <> wrote in message news:...
    | wrote:
    | >
    | >> 2. If 1 is no, does that mean that I need to do all the manipulation in
    | >> some icky string format and then go back?
    | >
    | >I use:
    | >
    | >((high << 16) | low)
    |
    | ...which, unfortunately, gives a FutureWarning in Python 2.3 if high
    | happens to be larger than 32767.

    Yes, it's really annoying. I use (high * 65536 + low) instead.

    -Georgy
    Georgy Pruss, Dec 3, 2003
    #3
  4. Bill Sneddon Guest

    Georgy Pruss wrote:
    > "Tim Roberts" <> wrote in message news:...
    > | wrote:
    > | >
    > | >> 2. If 1 is no, does that mean that I need to do all the manipulation in
    > | >> some icky string format and then go back?
    > | >
    > | >I use:
    > | >
    > | >((high << 16) | low)
    > |
    > | ...which, unfortunately, gives a FutureWarning in Python 2.3 if high
    > | happens to be larger than 32767.
    >
    > Yes, it's really annoying. I use (high * 65536 + low) instead.
    >
    > -Georgy
    >
    >

    Were are you getting you numbers from?
    Have you looked at the struct module?

    I am reading a binary file and use this command from struct module to
    convert to integer not sure if this helps you or not.

    mwB = struct.unpack('>H',mw)[0] # unpack big-endian unsigned short
    ...detuple it
    Bill Sneddon, Dec 3, 2003
    #4
  5. Thanks everybody.

    I settled on the
    ((high << 8) | low)
    As this fits my need for the moment.

    I will look at struct when I have some more time.

    Thanks,
    Matt
    Matthew A. Berglund, Dec 6, 2003
    #5
  6. Tim Roberts Guest

    "Matthew A. Berglund" <> wrote:
    >
    >Thanks everybody.
    >
    >I settled on the
    >((high << 8) | low)
    >As this fits my need for the moment.


    But this solves a very different problem from the one you originally
    posted, which was ((high << 16) | low). If you need to combine WORDS, then
    the solution you have settled on will produce the wrong answers.
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Dec 7, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,770
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Matthew A. Berglund

    Binary number manipulation

    Matthew A. Berglund, Nov 28, 2003, in forum: Python
    Replies:
    1
    Views:
    373
    Irmen de Jong
    Nov 28, 2003
  3. nguser3552
    Replies:
    3
    Views:
    441
  4. Bob Sanders
    Replies:
    5
    Views:
    216
    Ryan Lewis
    May 30, 2008
  5. Chris

    Manipulation binary files

    Chris, Oct 19, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    157
    Anno Siegel
    Oct 23, 2003
Loading...

Share This Page