Google interview question

Given a base 10 number, print the hexidecimal (base 16) representation of that number.

Interview Answers

Anonymous

14 Mar 2012

String buffer = new StringBuffer(); while (n > 0) { int base = n% 16; buffer.append((base<10 ? base : (Char)('a'+base-10)); n /= 16; } String result = buffer.toString().reverse();

4

Anonymous

10 Nov 2016

public String toHex(int num) { if(num == 0) return "0"; char[] map = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; String result = ""; while(num != 0){ System.out.println((num & 15)+ " "+ result); result = map[(num & 15)] + result; num = (num >>> 4); } return result.toString(); }

1

Anonymous

25 Oct 2013

To any base. private static String toBase(int number, int base, boolean literal){ String baseResult=""; while(number > 0){ int n = number % base; if(base == 16 && n > 9 && n < 16){ baseResult += getBase16Char(n); }else{ baseResult =n + baseResult; } number /=base; } return literal ? baseToLiteral(base).concat(baseResult) : baseResult; } private static String baseToLiteral(int base){ switch(base){ case 2: return "b"; case 8: return "o"; case 10: return "d"; case 16: return "x"; default: return "("+base+")"; } } private static char getBase16Char(int digit){ char[] base16char = new char[16]; base16char[10] ='a'; base16char[11] = 'b'; base16char[12] = 'c'; base16char[13] = 'd'; base16char[14] = 'e'; base16char[15] = 'f'; return base16char[digit]; }

Anonymous

25 Oct 2013

To any base. private static String anyToBase(int number, int base, boolean literal){ String baseResult=""; while(number > 0){ int n = number % base; if(base == 16 && n > 9 && n < 16){ baseResult = getBase16Char(n) + baseResult; }else{ baseResult =n + baseResult; } number /=base; } return literal ? baseToLiteral(base).concat(baseResult) : baseResult; } private static String baseToLiteral(int base){ switch(base){ case 2: return "0b"; case 8: return "0o"; case 10: return "0d"; case 16: return "0x"; default: return "0("+base+")"; } } private static char getBase16Char(int digit){ char[] base16char = new char[16]; base16char[10] ='a'; base16char[11] = 'b'; base16char[12] = 'c'; base16char[13] = 'd'; base16char[14] = 'e'; base16char[15] = 'f'; return base16char[digit]; }

Anonymous

22 Oct 2011

public void printHex(int d) { string s = ""; int m = 0; int next = d; int a[16]; a[10] = "a"; a[11] = "b"; a[12] = "c"; a[13] = "d"; a[14] = "e"; a[15] = "f"; while (next > 15) { m = next % 16; if (m > 9 && m < 16) s = a[m] + s; else s = m + s; next = next / 16; } system.out.println(s); }

Anonymous

4 Feb 2012

B's answer doesn't work. I think a quick fix (besides all of the issues like incorrect array initialization, setting int to string, etc) would be to change while loop to a do while and the conditional to next > 0. Here is a generic solution passing radix which also handles negative numbers: public static String intToStr(int val, int radix) { char[] digits = new char[36]; for (int i = 0; i < 10; i++) { digits[i] = (char)('0' + i); } for (int i = 10; i < 36; i++) { digits[i] = (char)('A' + i); } String result = ""; boolean negative = false; if (val < 0) { negative = true; } val = Math.abs(val); do { result = digits[val%radix] + result; val = val/radix; } while (val != 0); if (negative) { result = "-" + result; } return result; }