Chapter 3 python3, python3
Let's first recall the previous knowledge of hexadecimal conversion (see chapter 10). The 10-hexadecimal conversion method is as follows:
- Integer part, except for the base remainder, arranged in reverse order
- Decimal part, rounded by base, ordered
- Negative, Processed in absolute value
Well, assume that all the numbers to be converted are positive integers, then this function can be written as follows:
Def Convert1 (num: 'decimal number', base: 'convert to the hexadecimal number')-> 'converted number ': # index = ["0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ', 'Z'] res = "" # used to store the result while num> = base: num1 = num // base # quotient num2 = num % base # remainder num = num1 res = str (index [num2]) + res else: res = str (num) + res return res
Add the code to call the function:
Def Convert1 (num: 'decimal number', base: 'convert to the hexadecimal number')-> 'converted number ': # index = ["0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ', 'Z'] res = "" # used to store the result while num> = base: num1 = num // base # quotient num2 = num % base # remainder num = num1 res = str (index [num2]) + res else: res = str (num) + Res return resnum = input ('enter a decimal number and press Enter: ') # decimal number num = int (num) base = input (' enter a 2 ~ 36 digits are in hexadecimal format, and press Enter: ') # convert to this hexadecimal base = int (base) print (Convert1 (num, base ))
Run the code, enter 100 and 26 respectively, and program output:
Enter a decimal number and press Enter: 100 enter a 2 ~ 36 numbers are in hexadecimal format, and press Enter: 263 M
Online verification tool: http://tool.oschina.net/hexconvert/ verification is correct.
Now, assume that the numbers to be converted are decimal places between 0 and 1:
Def Convert2 (num: 'decimal number between 0 and 1', base: 'convert to this hexadecimal number')-> 'converted number ': # index = ["0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ', 'Z'] res = "0. "# used to store the result while (num <1) & (num> 0): num1 = num * base # product num2 = int (num1 // 1) # integer num = num1-num2 res + = str (index [num2]) If len (res)> 10: # Keep Only eight bits of break return resnum = input ('enter a decimal number and press Enter :') # decimal number num = float (num) base = input ('enter a 2 ~ 36 digits are in hexadecimal format, and press Enter: ') # convert to this hexadecimal base = int (base) print (Convert2 (num, base ))
Thinking: What will happen if the while clause does not determine the number of digits?
Next, we only need to combine the integer and decimal parts to obtain the final function:
Def Convert1 (num: 'decimal number', base: 'convert to the hexadecimal number')-> 'converted number ': # index = ["0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y ', 'Z'] res = "" # used to store the result while num> = base: num1 = num // base # quotient num2 = num % base # remainder num = num1 res = str (index [num2]) + res else: res = str (num) + Res return resdef Convert2 (num: 'Number in decimal format from 0 to 1', base: 'Number converted to this hexadecimal number')-> 'Number converted ': # index = ["0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", 'F', 'G ', 'H', 'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P', 'Q ', 'R', 's', 't', 'U', 'V', 'w', 'x', 'y', 'z'] res = ". "# It is used to store the result. If the result is not merged, It is 0. think about why change it. while (num <1) & (num> 0): num1 = num * base # product num2 = int (num1 // 1) # integer Num = num1-num2 res + = str (index [num2]) if len (res)> 10: # Keep only 8 bits break return resdef Convert (num, base ): ": param num: decimal number: param base: to be converted to this hexadecimal number: return the number after conversion" numConvert = abs (num) # obtain the absolute value num1 = int (numConvert // 1) of num # obtain the integer part num2 = numConvert-num1 # obtain the fractional part if num2 = 0: res = Convert1 (num1, base) else: res = Convert1 (num1, base) + Convert2 (num2, base) return res # Call the function num = input ('Enter a decimal number, and press Enter: ') # decimal number num = float (num) base = input (' enter a 2 ~ 36 digits are in hexadecimal format, and press Enter: ') # Convert to this hexadecimal base = int (base) print (Convert (num, base ))
At this point, the function has been written, but we find that this function is not perfect, because we must ensure that the input 10-digit number is correct, otherwise the function will report an error. Please think: how can I improve functions using the knowledge learned in the previous sections and prompt the user to re-enter the functions when the user enters an incorrect format? Next, let's talk about how to deal with this situation.