hex notation funtion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tertius

    #1

    hex notation funtion

    Hi,

    Is there a builtin function that will enable me to display the hex
    notation of a given binary string? (example below)

    many thanks
    Tertius



    0000(0000) 02 11 00 00 46 5A 1A 82 02 11 00 39 36 39 33 39
    .....FZ.....969 39

    0016(0010) 36 39 33 00 0A 30 33 37 34 34 39 35 38 25 DD 01
    693..03744958%. .
  • Irmen de Jong

    #2
    Re: hex notation funtion

    tertius wrote:[color=blue]
    > Hi,
    >
    > Is there a builtin function that will enable me to display the hex
    > notation of a given binary string? (example below)[/color]

    Does this help:
    [color=blue][color=green][color=darkred]
    >>> "hello".encode( "hex")[/color][/color][/color]
    '68656c6c6f'[color=blue][color=green][color=darkred]
    >>> "deadbeef".deco de("hex")[/color][/color][/color]
    '\xde\xad\xbe\x ef'

    ?

    --Irmen

    Comment

    • Philippe C. Martin

      #3
      Re: hex notation funtion

      Would that do it?

      for i in my_byte_string:
      .... = atoi(binascii.h exlify(i),16)

      Regards,

      Philippe





      On Tue, 18 Jan 2005 20:43:44 +0200, tertius wrote:
      [color=blue]
      > Hi,
      >
      > Is there a builtin function that will enable me to display the hex
      > notation of a given binary string? (example below)
      >
      > many thanks
      > Tertius
      >
      >
      >
      > 0000(0000) 02 11 00 00 46 5A 1A 82 02 11 00 39 36 39 33 39
      > ....FZ.....9693 9
      >
      > 0016(0010) 36 39 33 00 0A 30 33 37 34 34 39 35 38 25 DD 01
      > 693..03744958%. .[/color]

      Comment

      • wittempj@hotmail.com

        #4
        Re: hex notation funtion

        This will do it:[color=blue][color=green][color=darkred]
        >>> int('10000000', 2)[/color][/color][/color]
        128[color=blue][color=green][color=darkred]
        >>> hex(int('100000 00', 2))[/color][/color][/color]
        '0x80'[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • Grant Edwards

          #5
          Re: hex notation funtion

          On 2005-01-18, tertius <terti@mighty.c o.za> wrote:
          [color=blue]
          > Is there a builtin function that will enable me to display the hex
          > notation of a given binary string? (example below)[/color]

          ' '.join('%02x' % ord(b) for b in s)

          --
          Grant Edwards grante Yow! This is a NO-FRILLS
          at flight -- hold th' CANADIAN
          visi.com BACON!!

          Comment

          • Grant Edwards

            #6
            Re: hex notation funtion

            On 2005-01-18, Grant Edwards <grante@visi.co m> wrote:[color=blue]
            > On 2005-01-18, tertius <terti@mighty.c o.za> wrote:
            >[color=green]
            >> Is there a builtin function that will enable me to display the hex
            >> notation of a given binary string? (example below)[/color]
            >
            > ' '.join('%02x' % ord(b) for b in s)[/color]

            Oops. Should be:

            ' '.join(['%02x' % ord(b) for b in s])

            --
            Grant Edwards grante Yow! .. Am I in a SOAP
            at OPERA??
            visi.com

            Comment

            • Peter Hansen

              #7
              Re: hex notation funtion

              Grant Edwards wrote:[color=blue]
              > On 2005-01-18, Grant Edwards <grante@visi.co m> wrote:
              >[color=green]
              >>On 2005-01-18, tertius <terti@mighty.c o.za> wrote:
              >>
              >>[color=darkred]
              >>>Is there a builtin function that will enable me to display the hex
              >>>notation of a given binary string? (example below)[/color]
              >>
              >>' '.join('%02x' % ord(b) for b in s)[/color]
              >
              >
              > Oops. Should be:
              >
              > ' '.join(['%02x' % ord(b) for b in s])[/color]

              The first works fine under Python 2.4, actually... you
              need the list comprehension only on previous versions.

              -Peter

              Comment

              • tertius

                #8
                Re: hex notation funtion

                tertius wrote:[color=blue]
                > Hi,
                >
                > Is there a builtin function that will enable me to display the hex
                > notation of a given binary string? (example below)
                >[/color]

                Thanks all.

                Comment

                Working...