Click to See Complete Forum and Search --> : Convert value to hexadecimal in jsp
annie519
07-24-2003, 06:16 AM
This sounds simple, but I can't seem to convert a value on the form (provided it's a valid number) to a hexadecimal. I've tried using the toHexString function, but it's not working. I can do it in a java servlet with no problem, but I'd like to be able to do it in a jsp.
This is what I currently have that doesn't work:
function ConvertToHex(obj)
{
res = obj.value;
hval = res.toHexString();
alert ("hval = "+hval);
form1.source.value = hval;
}
Any help would be greatly appreciated!!
Is this any help to you
<script>
var hexChars = "0123456789ABCDEF";
function Dec2Hex(Dec) {
var a = Dec % 16;
var b = (Dec - a)/16;
hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
alert(hex)
}
</script>
<a href="#null" onclick="Dec2Hex(125)">CLICK</a>
Charles
07-24-2003, 06:58 AM
1) The "type" attribute is required with the SCRIPT element.
2) This sort of thing is built into the Number.toString() method. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/number.html#1193464.
3) And see http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html.
<script type="text/javascript">
<!--
alert((255).toString(16))
// -->
</script>
annie519
07-24-2003, 08:09 AM
The Dec2Hex function definitely helps, but I used that before. I was hoping that there was a way I could strictly use java utilities to convert the value. Is this the only way to do it - to create and use my own "Dec2Hex" function?
Also, the toString(16) function just returns the value as a hex value. It's still the same number you put in. It doesn't inherently change the value from decimal to hex.
how about this way
<script type="text/javascript">
<!--
num=255
num=num.toString(16)
alert(num)
// -->
</script>
Or this
<script type="text/javascript">
<!--
function ConvertToHex(obj){
res = obj
hval = res.toString(16);
alert ("hval = "+hval);
//form1.source.value = hval;
}
// -->
</script>
<a href="#null" onclick="ConvertToHex(255)">num</a>
May have it here
<script type="text/javascript">
<!--
function ConvertToHex(obj){
res = obj.value
res=res*1
hval = res.toString(16);
form1.source.value = hval;
}
// -->
</script>
<form name="form1">
<input type="text" name="t1" value="Enter a number">
<input type="button" value="Convert" onclick="ConvertToHex(this.form.t1)">
<P><input type="text" name="source">
</form>
Charles
07-24-2003, 09:25 AM
Originally posted by annie519
Also, the toString(16) function just returns the value as a hex value. It's still the same number you put in. It doesn't inherently change the value from decimal to hex. You've lost me there, returning the value as a hex value is inherently changing the value from decimal to hex. Or do yo not know what to do with it?
foo = 25;
foo = foo.toString(16);
<input type="text" onchange="this.value = Number(this.value).toString(16)">
annie519
07-24-2003, 09:39 AM
MrJ - This did it:
<!--
function ConvertToHex(obj){
res = obj.value
res=res*1
hval = res.toString(16);
form1.source.value = hval;
}
Thanks so much for your help!!!