You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: easyPythonpi/easyPythonpi.py
+88-1Lines changed: 88 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,4 @@
1
+
importregexasre
1
2
""" A python module that helps you to calculate some of the most used calculations.....
2
3
usage--
3
4
Just download the file from git and unzip in ur system.
@@ -128,7 +129,87 @@ def bin2dec(x:'bin')->'dec':
128
129
129
130
a=a+1
130
131
returnr
131
-
132
+
133
+
# A function that converts a binary string to hexadecimal
134
+
defbin2hex(x:'bin')->'hex':
135
+
"""Converts a binary string to a hexadecimal string.
136
+
137
+
This function converts a binary string to hexadecimal. If the binary
138
+
string's length is a multiple of 4, simply break up the string into
139
+
substrings of 4 binary digits and determine their hexadecimal digit.
140
+
If the binary string's length not a multiple of 4 prepend the
141
+
necessary number of 0's to make the string a multiple of 4.
142
+
143
+
Args:
144
+
x ('bin') : Binary string to convert to hexadecimal.
145
+
146
+
Returns:
147
+
The binary converted to hexadecimal.
148
+
149
+
Raises:
150
+
No errors
151
+
"""
152
+
153
+
h=''# hexadecimal number converted from binary and to be returned
154
+
155
+
156
+
x=str(x)
157
+
158
+
# Determine if the string has invalid characters
159
+
ifre.search('[^(0-1)]', x):
160
+
raiseInvalidBinaryException
161
+
162
+
# Get the length of the string
163
+
l=len(x)
164
+
165
+
# Begin the process of converting x to its hexadecimal number
166
+
167
+
# If the length is not a multiple of 4, prepend 0's before converting
168
+
ifl%4!=0:
169
+
numZerosPrepended=4- ( l%4 ) # number of zeros to prepend
170
+
x= (numZerosPrepended*'0') +x# concatenate numZerosPrepended to x
171
+
172
+
173
+
foriinrange(len(x)-1, 0, -4):
174
+
substring=x[i-3:i+1] # The substring converted to a hex character
175
+
176
+
# Determine the binary substring's hex and prepend to h
177
+
ifsubstring=='0000':
178
+
h='0'+h
179
+
elifsubstring=='0001':
180
+
h='1'+h
181
+
elifsubstring=='0010':
182
+
h='2'+h
183
+
elifsubstring=='0011':
184
+
h='3'+h
185
+
elifsubstring=='0100':
186
+
h='4'+h
187
+
elifsubstring=='0101':
188
+
h='5'+h
189
+
elifsubstring=='0110':
190
+
h='6'+h
191
+
elifsubstring=='0111':
192
+
h='7'+h
193
+
elifsubstring=='1000':
194
+
h='8'+h
195
+
elifsubstring=='1001':
196
+
h='9'+h
197
+
elifsubstring=='1010':
198
+
h='A'+h
199
+
elifsubstring=='1011':
200
+
h='B'+h
201
+
elifsubstring=='1100':
202
+
h='C'+h
203
+
elifsubstring=='1100':
204
+
h='C'+h
205
+
elifsubstring=='1101':
206
+
h='D'+h
207
+
elifsubstring=='1110':
208
+
h='E'+h
209
+
elifsubstring=='1111':
210
+
h='F'+h
211
+
212
+
returnh
132
213
133
214
defcreatearray(length:'int',dtype='int')->'array': # To create an array of entered length and entered data type(interger data type is a default data type)
0 commit comments