3
3
4
4
"""
5
5
from ..types import is_bin , is_bytes , is_hex , is_int , is_list , is_pos_int , is_str
6
- from ..utils import BitArray as Bits
7
6
from ...compat import b , ensure_str
8
7
9
8
for _m in ["binascii" , "functools" , "math" ]:
@@ -91,10 +90,12 @@ def bin2hex(binary_string, nbits_in=8, nbits_out=8) -> str:
91
90
def bin2int (binary_string , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> int :
92
91
""" Convert a binary string (eventually using a separator) to an integer, using a given number of bits and in little
93
92
or big endian. """
94
- bs = binary_string
93
+ bs , n_b , n_B = binary_string , nbits_in , nbits_out
95
94
__validation (b = bs , o = order , u = unsigned )
96
- bs , pref = Bits (bin2bin (bs , nbits_in , nbits_out )), ["" , "u" ][unsigned ]
97
- return getattr (bs , pref + ("intle" if order == "little" else "intbe" ))
95
+ bs = bin2bin (bs , n_b , n_B )
96
+ if order == "little" :
97
+ bs = '' .join (reversed ([bs [i :i + n_B ] for i in range (0 , len (bs ), n_B )]))
98
+ return int (bs , 2 ) - (1 << len (bs )) if not unsigned and bs [0 ] == "1" else int (bs , 2 )
98
99
99
100
100
101
@__ensure_bitstring
@@ -117,61 +118,44 @@ def hex2bin(hex_string, nbits_in=8, nbits_out=8) -> str:
117
118
return bin2bin (padz (bs , n_b ), n_b , n_B )
118
119
119
120
120
- def hex2int (hex_string , order = "big" , unsigned = True ) -> int :
121
+ def hex2int (hex_string , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> int :
121
122
""" Convert a hexadecimal string to a big integer. """
122
- h = hex_string
123
- __validation (h = h , o = order , u = unsigned )
124
- bs = Bits ()
125
- bs .hex = h
126
- pref = ["" , "u" ][unsigned ]
127
- return getattr (bs , pref + ("intle" if order == "little" else "intbe" ))
123
+ n_B = nbits_out
124
+ return bin2int (hex2bin (hex_string , nbits_in , n_B ), n_B , n_B , order , unsigned )
128
125
129
126
130
127
def hex2str (hex_string ) -> str :
131
128
""" Convert a hexadecimal string to a string of 8-bits characters. """
132
129
h = hex_string
133
130
__validation (h = h )
134
- return ensure_str (binascii .unhexlify (b (hex_string )))
131
+ return ensure_str (binascii .unhexlify (b (h )))
135
132
136
133
137
134
# INTEGER ==> *
138
135
def int2bin (integer , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> str :
139
136
""" Convert an integer to a binary string in little or big endian. """
140
- i = integer
141
- __validation (i = i , n_b = nbits_in , n_B = nbits_out , o = order , u = unsigned )
142
- bs = Bits ()
143
- nbits = i . bit_length () + int ( not unsigned )
144
- bs . hex = int ( math . ceil ( max ( nbits , 1 ) / 8.0 )) * 2 * "0"
145
- pref = [ "" , "u" ][ unsigned ]
146
- setattr ( bs , pref + ("intle" if order == "little" else "intbe" ), i )
147
- bs . _nbits = nbits_in
148
- bs . nbits = nbits_out
149
- return bs . bin
150
-
151
-
152
- def int2hex (integer , order = "big" , unsigned = True ) -> str :
137
+ i , n_b , n_B = integer , nbits_in , nbits_out
138
+ __validation (i = i , n_b = n_b , n_B = nbits_out , o = order , u = unsigned )
139
+ if not unsigned and i < 0 :
140
+ i = ( 1 << n_b ) + i
141
+ bs = format ( i , f'0 { n_b } b' )
142
+ bs = "" . join ( bs [ i : i + n_b ] for i in range ( 0 , len ( bs ), n_b ))
143
+ bs = bs . zfill ( len ( bs ) + (n_b - ( len ( bs ) % n_b )) % n_b )
144
+ if order == "little" :
145
+ bs = "" . join ( reversed ([ bs [ i : i + n_b ] for i in range ( 0 , len ( bs ), n_b )]))
146
+ return bin2bin ( bs , n_b , n_B )
147
+
148
+
149
+ def int2hex (integer , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> str :
153
150
""" Convert an integer to a hexadecimal string. """
154
- i = integer
155
- __validation (i = i , o = order , u = unsigned )
156
- bs = Bits ()
157
- nbits = i .bit_length () + int (not unsigned )
158
- bs .hex = int (math .ceil (max (nbits , 1 ) / 8.0 )) * 2 * "0"
159
- pref = ["" , "u" ][unsigned ]
160
- setattr (bs , pref + ("intle" if order == "little" else "intbe" ), i )
161
- return bs .hex
162
- #"".join([h[i:i+2] for i in range(0, len(h), 2)][::-1])
151
+ n_B = nbits_out
152
+ return bin2hex (int2bin (integer , nbits_in , n_B , order , unsigned ), n_B , n_B )
163
153
164
154
165
- def int2str (integer , order = "big" , unsigned = True ) -> str :
155
+ def int2str (integer , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> str :
166
156
""" Convert a big integer to a string of 8-bits characters. """
167
- i = integer
168
- __validation (i = i , o = order , u = unsigned )
169
- bs = Bits ()
170
- nbits = i .bit_length () + int (not unsigned )
171
- bs .hex = int (math .ceil (max (nbits , 1 ) / 8.0 )) * 2 * "0"
172
- pref = ["" , "u" ][unsigned ]
173
- setattr (bs , pref + ("intle" if order == "little" else "intbe" ), i )
174
- return ensure_str (bs .bytes )
157
+ n_B = nbits_out
158
+ return bin2str (int2bin (integer , nbits_in , n_B , order , unsigned ), n_B , n_B )
175
159
176
160
177
161
def int2uni (integer ) -> str :
@@ -202,15 +186,11 @@ def str2hex(chars_string) -> str:
202
186
return ensure_str (binascii .hexlify (b (s )))
203
187
204
188
205
- def str2int (chars_string , order = "big" , unsigned = True ) -> int :
189
+ def str2int (chars_string , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> int :
206
190
""" Convert a string of 8-bits characters to a big integer or, if using blocks of nchars characters, a list of big
207
191
integers. """
208
- s = chars_string
209
- __validation (s = s , o = order , u = unsigned )
210
- bs = Bits ()
211
- bs .bytes = b (s )
212
- pref = ["" , "u" ][unsigned ]
213
- return getattr (bs , pref + ("intle" if order == "little" else "intbe" ))
192
+ n_B = nbits_out
193
+ return bin2int (str2bin (chars_string , nbits_in , n_B ), n_B , n_B , order , unsigned )
214
194
215
195
216
196
def str2lst (chars_string ) -> list :
0 commit comments