33
44"""
55from ..types import is_bin , is_bytes , is_hex , is_int , is_list , is_pos_int , is_str
6- from ..utils import BitArray as Bits
76from ...compat import b , ensure_str
87
98for _m in ["binascii" , "functools" , "math" ]:
@@ -91,10 +90,12 @@ def bin2hex(binary_string, nbits_in=8, nbits_out=8) -> str:
9190def bin2int (binary_string , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> int :
9291 """ Convert a binary string (eventually using a separator) to an integer, using a given number of bits and in little
9392 or big endian. """
94- bs = binary_string
93+ bs , n_b , n_B = binary_string , nbits_in , nbits_out
9594 __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 )
9899
99100
100101@__ensure_bitstring
@@ -117,61 +118,44 @@ def hex2bin(hex_string, nbits_in=8, nbits_out=8) -> str:
117118 return bin2bin (padz (bs , n_b ), n_b , n_B )
118119
119120
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 :
121122 """ 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 )
128125
129126
130127def hex2str (hex_string ) -> str :
131128 """ Convert a hexadecimal string to a string of 8-bits characters. """
132129 h = hex_string
133130 __validation (h = h )
134- return ensure_str (binascii .unhexlify (b (hex_string )))
131+ return ensure_str (binascii .unhexlify (b (h )))
135132
136133
137134# INTEGER ==> *
138135def int2bin (integer , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> str :
139136 """ 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 :
153150 """ 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 )
163153
164154
165- def int2str (integer , order = "big" , unsigned = True ) -> str :
155+ def int2str (integer , nbits_in = 8 , nbits_out = 8 , order = "big" , unsigned = True ) -> str :
166156 """ 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 )
175159
176160
177161def int2uni (integer ) -> str :
@@ -202,15 +186,11 @@ def str2hex(chars_string) -> str:
202186 return ensure_str (binascii .hexlify (b (s )))
203187
204188
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 :
206190 """ Convert a string of 8-bits characters to a big integer or, if using blocks of nchars characters, a list of big
207191 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 )
214194
215195
216196def str2lst (chars_string ) -> list :
0 commit comments