How would an average programmer working in industry convert an integer to hexadecimal in Javascript?
He would assume that he is not the first one in having the problem. He'd bet Javascript itself has a specific function or some standard library out there solves the problem.
Just googling around he would find a quick, clean and nice answer such as this one:
yourNum = yourNum.toString(16);
And, what about the average academician?
He would probably spend a whole day building and testing an impossible-to-understand, prone-to-error implementation.
Here is some Javascript I found at the website of some top-researchers in knowledge representation from a well-known university.
// *** Hexidecimal/Decimal Functions (needed to determine QuickTime version number from VBScript) *** function tohex(i) { a2 = '' ihex = hexQuot(i); idiff = eval(i + '-(' + ihex + '*16)') a2 = itohex(idiff) + a2; while( ihex > 16) { itmp = hexQuot(ihex); idiff = eval(ihex + '-(' + itmp + '*16)'); a2 = itohex(idiff) + a2; ihex = itmp; } a1 = itohex(ihex); return a1 + a2 ; } function hexQuot(i) { return Math.floor(eval(i +'/16')); } function itohex(i) { if( i == 0) { aa = '0' } else { if( i== 1) { aa = '1'} else {if( i== 2) { aa = '2'} else {if( i == 3) { aa = '3' } else {if( i== 4) { aa = '4'} else {if( i == 5) { aa = '5' } else {if( i== 6) { aa = '6'} else {if( i == 7) { aa = '7' } else {if( i== 8) { aa = '8'} else {if( i == 9) { aa = '9'} else {if( i==10) { aa = 'A'} else {if( i==11) { aa = 'B'} else {if( i==12) { aa = 'C'} else {if( i==13) { aa = 'D'} else {if( i==14) { aa = 'E'} else {if( i==15) { aa = 'F'} } } } } } } } } } } } } } } } return aa }
Weird.
No comments:
Post a Comment