PDA

View Full Version : decimal to hex (is there another way?)


swmr
05-03-2003, 06:00 AM
How would one convert color, using javascript, to get results like so?
--------------------
16711680 = #0000ff

255 = #ff0000
--------------------
So far, all scripts I've found do just the opposite.

The problem here is that Internet Explorer outputs those
decimal numbers (for those colors) when I try to do something like:

document.querryCommandValue("ForeColor")...

then when I use a ready-made javascript (to convert decimal to hex), I end up with the opposite colors for those values: :rolleyes:
--------------------
16711680 = #ff0000

255 = #0000ff
--------------------

hmm :confused:

beetle
05-03-2003, 06:38 AM
Well, 255 doesnt equal #FF0000, and it DOES equal #0000FF

Now, 255,0,0 equals #FF0000. Do you have the 8-bit decimal value for R, G, and B?

shlagish
05-03-2003, 06:42 PM
This script converts from any TO any radix (2 to 36).
You could just tell to go from dec to hex by putting the numer in the first input box, 10 in the second (for dec) and 16 in the third (for hex)


<html>
<body>
<script language="JavaScript">
function convert(input, origin, dest)
{
if (origin>36 || dest>36 || origin<2 || dest<2) return "Invalid numbering system"
return parseInt(input, origin||10).toString(dest||10);
}
</script>
<form>
<table><tr>
<td colspan="2"><b>Numbers converter</b><br><small>Converts numbers between different numbering scales<br>Please input the relevant information in the boxes</small></td>
</tr>
<tr height="1"><td colspan="2" bgcolor="black"></td></tr>
<tr>
<td>Number to convert:<br><small>May hold numbers as well as letters</small></td>
<td><input type="text" name="originalNumber"></td>
</tr>
<tr height="1"><td colspan="2" bgcolor="gray"></td></tr>
<tr>
<td>Scale to convert from:<br><small>Fx. 2 for binary or 16 for hexadecimal<br>min. 2, max. <script>document.write(convertBase.length)</script></small></td>
<td><input type="text" name="originalScale"></td>
</tr>
<tr height="1"><td colspan="2" bgcolor="gray"></td></tr>
<tr>
<td>Scale to convert to:</td>
<td><input type="text" name="destinationScale"></td>
</tr>
<tr height="1"><td colspan="2" bgcolor="gray"></td></tr>
<tr>
<td><input type="button" onClick='destinationNumber.value=convert(originalNumber.value,originalScale.value,destinationScale.v alue)' value="Convert"></td>
<td><input type="text" name="destinationNumber" value=""></td>
</tr></table>

</form>

</body>
</html>


With this script, 255 will return ff
If you want green, put ff in the middle of your hex number (00ff00)
red->ff0000
blue->0000ff

Basically, find each value seperatly

swmr
05-03-2003, 07:05 PM
Thank you; that will be a good learning tool :thumbsup:

-----------------------------------------------------------------

I happened to find just what I was looking for within the code of this page: Update Journal (http://fordi.sytes.net/fordi/nulj/oldlj.html)

problem solved :)

shlagish
05-03-2003, 08:54 PM
for the line:
document.write(convertBase.length)
Just write 36 instead, it was part of an old version of the script I had that used a variable (convertBase) wich was a string of 36 characters (0123456789abcdefghijklmnopqrstuvwxyz)
so just write: 36
instead of: <script>document.write(convertBase.length)</script>

swmr
05-03-2003, 09:19 PM
Ok, thanks.