hex string into binary format?

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

    hex string into binary format?

    Hi,

    How do I get a hexvalued string to a format recognized for binary
    calculation?


    import binascii
    s1 = '1C46BE3D9F6AA8 20'
    s2 = '8667B5236D89CD 46'

    i1 = binascii.unhexl ify(s1)
    i2 = binascii.unhexl ify(s2)
    x = i1 ^i2

    TypeError: unsupported operand type(s) for ^: 'str' and 'str'

    Many TIA
    T
  • Harry George

    #2
    Re: hex string into binary format?

    "Tertius Cronje" <Tertius.Cronje @postoffice.co. za> writes:
    [color=blue]
    > Hi,
    >
    > How do I get a hexvalued string to a format recognized for binary
    > calculation?
    >
    >
    > import binascii
    > s1 = '1C46BE3D9F6AA8 20'
    > s2 = '8667B5236D89CD 46'
    >
    > i1 = binascii.unhexl ify(s1)
    > i2 = binascii.unhexl ify(s2)
    > x = i1 ^i2
    >
    > TypeError: unsupported operand type(s) for ^: 'str' and 'str'
    >
    > Many TIA
    > T[/color]

    i1=int(s1,16)
    i2=int(s2,16)

    --
    harry.g.george@ boeing.com
    6-6M21 BCA CompArch Design Engineering
    Phone: (425) 294-4718

    Comment

    • Peter Hansen

      #3
      Re: hex string into binary format?

      Tertius Cronje wrote:[color=blue]
      > How do I get a hexvalued string to a format recognized for binary
      > calculation?
      >
      > import binascii
      > s1 = '1C46BE3D9F6AA8 20'
      > s2 = '8667B5236D89CD 46'
      >
      > i1 = binascii.unhexl ify(s1)
      > i2 = binascii.unhexl ify(s2)[/color]

      Try this instead:
      i1 = long(s1, 16)
      i2 = long(s2, 16)
      [color=blue]
      > x = i1 ^i2[/color]

      Comment

      • Tim Roberts

        #4
        Re: hex string into binary format?

        "Tertius Cronje" <Tertius.Cronje @postoffice.co. za> wrote:[color=blue]
        >
        >How do I get a hexvalued string to a format recognized for binary
        >calculation?[/color]

        You're going to be embarrassed.
        [color=blue]
        >import binascii
        >s1 = '1C46BE3D9F6AA8 20'
        >s2 = '8667B5236D89CD 46'
        >
        >i1 = binascii.unhexl ify(s1)
        >i2 = binascii.unhexl ify(s2)
        >x = i1 ^i2
        >
        > TypeError: unsupported operand type(s) for ^: 'str' and 'str'[/color]

        No imports at all:

        s1 = '1C46BE3D9F6AA8 20'
        s2 = '8667B5236D89CD 46'
        i1 = int(s1,16)
        i2 = int(s2,16)
        x = i1 ^ i2
        print hex(x)
        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        Working...