Google interview question

Convert a decimal integer to hex

Interview Answers

Anonymous

26 Nov 2014

Assuming the conversion is from string to string (e.g. "10" => "A"), we can take several steps to convert: 1. Convert number string to int or uint (assume str is a decimal number string): unsigned int num = 0; for (int i = 0; i > 4; if (num < 10) hexStr = (num+'0') + hexStr; else hexStr = (num+'A') + hexStr; }

1

Anonymous

26 Nov 2014

Made several mistakes above, sorry - fixed code below Assuming the conversion is from string to string (e.g. "10" => "A"), we can take several steps to convert: 1. Convert number string to int or uint (assume decStr is a decimal number string): unsigned int num = 0; for (int i = 0; i > 4; if (hex < 10) hexStr = (hex+'0') + hexStr; else hexStr = (hex - 10 +'A') + hexStr; // Need to subtract 10 }