views:

202

answers:

4

What is the hexadecimal equivalent of number 264 and 140 ? Please give me full coding of this program.

+1  A: 

264: 0x108

140: 0x8C

No program required. You're welcome.

Noon Silk
Did you do that by hand, or did you use the Calculator applet?
John Saunders
With the calculator of course. As a programmer I am obviously lazy.
Noon Silk
Hmm, maybe you had better do it programmatically after all. `s/246/264/`
Tim Sylvester
Right you are; though the hex was correct :) Corrected.
Noon Silk
+7  A: 

You can use the toString function on numbers, and pass the base to be converted:

(264).toString(16); // 108 hex
(140).toString(16); // 8c  hex

And to do the opposite, you can use the parseInt function:

parseInt('108', 16);  // 264 dec
parseInt('8c', 16);   // 140 dec
CMS
+1  A: 

If you didn't know, you can use Google to convert decimal to hexadecimal or vice versa.

But it looks like @CMS already posted the Javascript code for you :) +1 to him.

Mark Rushakoff
+1 for using Google's conversion tool
Randell
+1  A: 

Since this is a Homework question, I am guessing that the questioner may be looking for an algorithm.

This is some C# code that does this without special functions:

int x = 140;
string s = string.Empty;
while (x != 0)
{
    int hexadecimal = x % 16;
    if (hexadecimal < 10)
         s = hexadecimal.ToString() + s;
    else
         s = ((char)('A' + (char)(hexadecimal - 10))).ToString() + s;
    x = (int)(x / (int)16);
}
MessageBox.Show("0X" + s);

Since this is homework, you can figure out how to translate this into JavaScript

jrcs3