Question: Convert decimal to binary, decimal to hex This function inputs a positive integer in decimal, and outputs an array (vector)with hexadecimal digits long dec_hex(long a,
Convert decimal to binary, decimal to hex This function inputs a positive integer in decimal, and outputs an array (vector)with hexadecimal digits long dec_hex(long a, std: : vector & digits) { const long base = 16: char alphanum[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}: digits.clear(): if (a 0) { long run = a % base: char c = alphanun[run] digits.push_back(c): a /=base: } std: : reverse(digits.begin(), digits, end()): // reverse the digits return 0: } Note that the output vector is 'char' not 'int' because the digits are alphanumeric. Safety check: if a & digits) Convert decimal to binary, decimal to hex This function inputs a positive integer in decimal, and outputs an array (vector)with hexadecimal digits long dec_hex(long a, std: : vector & digits) { const long base = 16: char alphanum[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}: digits.clear(): if (a 0) { long run = a % base: char c = alphanun[run] digits.push_back(c): a /=base: } std: : reverse(digits.begin(), digits, end()): // reverse the digits return 0: } Note that the output vector is 'char' not 'int' because the digits are alphanumeric. Safety check: if a & digits)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
