Conversion between bytes and hexstr in Python3

Source: Internet
Author: User

1 conversion between bytes and hexstr in Python3
  1. Conversion of Bytetohex
    def bytetohex (bins): "" "    Convert a byte string to it's hex string representation e.g. for output.    " " Return '. Join (["%02x"% x for x in bins]). Strip ()
  2. Conversion of Hextobyte
    def hextobyte (hexstr): "" "    Convert a string hex byte values into a byte string. The Hex Byte values may or May is not    space separated.    "" Return Bytes.fromhex (HEXSTR)
  3. Test
    __hexstr1  = "ffffff5f8121070c0000ffffffff5f8129010b" __hexstr2  = "FF FF FF 5F", 0C XX, FF FF FF FF 5F 81 2 9 0B "__bytestr =" \xff\xff\xff\x5f\x81\x21\x07\x0c\x00\x00\xff\xff\xff\xff\x5f\x81\x29\x01\x0b "if __name__ = =" __ main__ ":    print (" \nhex to Byte and byte to Hex Conversion ")    print (" Test 1-bytetohex-passed: ", Bytetohex (__b  YTESTR) = = __hexstr2)    print ("Test 2-hextobyte-passed:", Hextobyte (__hexstr1) = = __bytestr)    print ("Test 3 -hextobyte-passed: ", Hextobyte (__hexstr2) = = __bytestr)    # turn a non-space separated hex string into a space se parated Hex string!    Print ("Test 4-combined  -Passed:",           Bytetohex (Hextobyte (__hexstr1)) = = __HEXSTR2)

Conversion between bytes and hexstr in Python3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.