<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                進行協議解析時,總是會遇到各種各樣的數據轉換的問題,從二進制到十進制,從字節串到整數等等 廢話不多上,直接上例子 整數之間的進制轉換: * 10進制轉16進制: hex(16) ?==> ?0x10 * 16進制轉10進制: int('0x10', 16) ?==> ?16 類似的還有oct(), bin() ------------------- 字符串轉整數: * 10進制字符串: int('10') ?==> ?10 * 16進制字符串: int('10', 16) ?==> ?16 * 16進制字符串: int('0x10', 16) ?==> ?16 ------------------- 字節串轉整數: * 轉義為short型整數: struct.unpack(' ?(1, 0) * 轉義為long型整數: struct.unpack(' ?(1,) ------------------- 整數轉字節串: * 轉為兩個字節: struct.pack(' ?b'\x01\x00\x02\x00' * 轉為四個字節: struct.pack(' ?b'\x01\x00\x00\x00\x02\x00\x00\x00' ------------------- 字符串轉字節串: * 字符串編碼為字節碼: '12abc'.encode('ascii') ?==> ?b'12abc' * 數字或字符數組: bytes([1,2, ord('1'),ord('2')]) ?==> ?b'\x01\x0212' * 16進制字符串: bytes().fromhex('010210') ?==> ?b'\x01\x02\x10' * 16進制字符串: bytes(map(ord, '\x01\x02\x31\x32')) ?==> ?b'\x01\x0212' * 16進制數組: bytes([0x01,0x02,0x31,0x32]) ?==> ?b'\x01\x0212' ------------------- 字節串轉字符串: * 字節碼解碼為字符串: bytes(b'\x31\x32\x61\x62').decode('ascii') ?==> ?12ab * 字節串轉16進制表示,夾帶ascii: str(bytes(b'\x01\x0212'))[2:-1] ?==> ?\x01\x0212 * 字節串轉16進制表示,固定兩個字符表示: str(binascii.b2a_hex(b'\x01\x0212'))[2:-1] ?==> ?01023132 * 字節串轉16進制數組: [hex(x) for x in bytes(b'\x01\x0212')] ?==> ?['0x1', '0x2', '0x31', '0x32'] =================== 測試用的python源碼 **[python]**?[view plain](http://blog.csdn.net/crylearner/article/details/38521685# "view plain")?[copy](http://blog.csdn.net/crylearner/article/details/38521685# "copy") 1. '''''? 2. Created?on?2014年8月21日? 3. ? 4. @author:?lenovo? 5. '''?? 6. import?binascii?? 7. import?struct?? 8. ?? 9. ?? 10. def?example(express,?result=None):?? 11. ????if?result?==?None:?? 12. ????????result?=?eval(express)?? 13. ????print(express,?'?==>?',?result)?? 14. ?? 15. ?? 16. if?__name__?==?'__main__':?? 17. ?????? 18. ????print('整數之間的進制轉換:')?? 19. ????print("10進制轉16進制",?end=':?');example("hex(16)")?? 20. ????print("16進制轉10進制",?end=':?');example("int('0x10',?16)")?? 21. ????print("類似的還有oct(),?bin()")?? 22. ?????? 23. ????print('\n-------------------\n')?? 24. ?????? 25. ????print('字符串轉整數:')?? 26. ????print("10進制字符串",?end=":?");example("int('10')")?? 27. ????print("16進制字符串",?end=":?");example("int('10',?16)")?? 28. ????print("16進制字符串",?end=":?");example("int('0x10',?16)")?? 29. ?????? 30. ????print('\n-------------------\n')?? 31. ?????? 32. ????print('字節串轉整數:')?? 33. ????print("轉義為short型整數",?end=":?");example(r"struct.unpack(')?? 34. ????print("轉義為long型整數",?end=":?");example(r"struct.unpack(')?? 35. ?? 36. ????print('\n-------------------\n')?? 37. ?? 38. ????print('整數轉字節串:')?? 39. ????print("轉為兩個字節",?end=":?");example("struct.pack(')?? 40. ????print("轉為四個字節",?end=":?");example("struct.pack(')?? 41. ?????? 42. ????print('\n-------------------\n')?? 43. ?????? 44. ????print('字符串轉字節串:')?? 45. ????print('字符串編碼為字節碼',?end=":?");example(r"'12abc'.encode('ascii')")?? 46. ????print('數字或字符數組',?end=":?");example(r"bytes([1,2,?ord('1'),ord('2')])")?? 47. ????print('16進制字符串',?end=':?');example(r"bytes().fromhex('010210')")?? 48. ????print('16進制字符串',?end=':?');example(r"bytes(map(ord,?'\x01\x02\x31\x32'))")?? 49. ????print('16進制數組',?end?=':?');example(r'bytes([0x01,0x02,0x31,0x32])')?? 50. ?????? 51. ????print('\n-------------------\n')?? 52. ?????? 53. ????print('字節串轉字符串:')?? 54. ????print('字節碼解碼為字符串',?end=":?");example(r"bytes(b'\x31\x32\x61\x62').decode('ascii')")?? 55. ????print('字節串轉16進制表示,夾帶ascii',?end=":?");example(r"str(bytes(b'\x01\x0212'))[2:-1]")?? 56. ????print('字節串轉16進制表示,固定兩個字符表示',?end=":?");example(r"str(binascii.b2a_hex(b'\x01\x0212'))[2:-1]")?? 57. ????print('字節串轉16進制數組',?end=":?");example(r"[hex(x)?for?x?in?bytes(b'\x01\x0212')]")?? 58. ?????? 59. ?????? 60. ????print('\n===================\n')?? 61. ????print("以上原理都比較簡單,看一下就明白了。這里僅僅是拋磚引玉,有更好更簡單的方法,歡迎歡迎")?? 以上原理都比較簡單,看一下就明白了。這里僅僅是拋磚引玉,有更好更簡單的方法,歡迎歡迎
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看