|
 beRARE blog
This is a very simple Decimal to Hexadecimal and Hexadecimal to Decimal Converter in JavaScript. Originally I built it
to quickly convert RGB colors schemes from Photoshop (decimal) to HTML (and the other way), but the new Photoshop provides this
service itself. Since people are actually using this script, I added a binary converter as well.
If you're interested in how simple it really is, this is the code:
<form>
<input type="text" name="dec" onBlur="hex.value=(this.value-0).toString(16)">(dec)
<input type="text" name="hex" onBlur="dec.value=parseInt(this.value,16)">(hex)
</form>
To convert the hexadecimal, I just use the parseInt function
with the additional parameter '16'. To convert from decimal to hexadecimal,
I use the toString method, with parameter '16'. But since the value in
the 'decimal' field is already a String object, it first needs to be casted
to an Int. I do this by subtracting zero (0). It then is a number and
it can be converted to a String, base 16.
|