conversion.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. def int_to_bytes(integer):
  19. """
  20. Convert an int to bytes
  21. :param integer:
  22. :return: bytes
  23. """
  24. return integer.to_bytes((integer.bit_length() + 7) // 8, 'big')
  25. # alternatively
  26. # return bytes.fromhex(hex(integer)[2:])
  27. def bytes_to_int(bytes_arr):
  28. """
  29. Convert bytes to an int
  30. :param bytes_arr:
  31. :return: int
  32. """
  33. return int.from_bytes(bytes_arr, byteorder='big', signed=False)
  34. def bytes_to_bin(bytes_arr):
  35. """
  36. Convert bytes to a binary number
  37. :param bytes_arr:
  38. :return: str, whose length must be a multiple of 8
  39. """
  40. res = bin(bytes_to_int(bytes_arr))[2:]
  41. return bin_compensate(res)
  42. def int_to_binary_representation(integer):
  43. """
  44. integer = 2^e1 + 2^e2 + ... + 2^ek, e1 > ... > ek
  45. :param integer: int
  46. :return: [e1, e2, ..., ek]
  47. """
  48. bin_str = bin(integer)[2:]
  49. bin_len = len(bin_str)
  50. exponent_list = []
  51. for i in range(bin_len):
  52. if bin_str[i] == '1':
  53. exponent_list.append(bin_len - i - 1)
  54. return exponent_list
  55. def str_to_bin(str_arr):
  56. """
  57. Convert a string to a binary number in string
  58. :param str_arr: str
  59. :return: str
  60. """
  61. res = ''
  62. for st in str_arr:
  63. char = bin(ord(st))[2:]
  64. res += bin_compensate(char)
  65. return res
  66. def bin_to_str(bin_str_arr):
  67. """
  68. Convert binary number in string to string
  69. :param bin_str_arr: str, whose length must be a multiple of 8
  70. :return: str
  71. """
  72. res = ''
  73. for i in range(0, len(bin_str_arr), 8):
  74. res += chr(int(bin_str_arr[i:i + 8], 2))
  75. return res
  76. def bin_compensate(bin_arr):
  77. """
  78. Compensate a binary number in string with zero till its length being a multiple of 8
  79. :param bin_arr: str
  80. :return: str
  81. """
  82. return '0' * (8 - len(bin_arr) % 8) + bin_arr
  83. def str_to_int(str_arr):
  84. """
  85. :param str_arr: str
  86. :return: int
  87. """
  88. return int(str_to_bin(str_arr), 2)
  89. def int_to_str(integer):
  90. """
  91. :param integer: int
  92. :return: str
  93. """
  94. return bin_to_str(bin_compensate(bin(integer)[2:]))
  95. def str_to_bytes(str_arr):
  96. """
  97. 'hello' -> b'hello'
  98. :param str_arr: str
  99. :return: bytes
  100. """
  101. return bytes(str_arr, 'utf-8')
  102. def bytes_to_str(byte_arr):
  103. """
  104. b'hello' -> 'hello'
  105. :param byte_arr: bytes
  106. :return: str
  107. """
  108. return str(byte_arr, 'utf-8')
  109. # return str(byte_arr, 'utf-8')