/    Sign up×
Community /Pin to ProfileBookmark

Convert value to hexadecimal in jsp

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!!

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JJul 24.2003 — 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>
Copy linkTweet thisAlerts:
@CharlesJul 24.2003 — [font=georgia]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.[/font]

[font=monospace]<script type="text/javascript">

<!--

alert((255).toString(16))

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@annie519authorJul 24.2003 — 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.
Copy linkTweet thisAlerts:
@Mr_JJul 24.2003 — how about this way

<script type="text/javascript">

<!--

num=255

num=num.toString(16)

alert(num)

// -->

</script>
Copy linkTweet thisAlerts:
@Mr_JJul 24.2003 — 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>
Copy linkTweet thisAlerts:
@Mr_JJul 24.2003 — 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>
Copy linkTweet thisAlerts:
@CharlesJul 24.2003 — [i]Originally posted by annie519 [/i]

[B]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. [/B][/QUOTE]
[font=georgia]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?[/font]

[font=monospace]foo = 25;

foo = foo.toString(16);

<input type="text" onchange="this.value = Number(this.value).toString(16)">[/font]
Copy linkTweet thisAlerts:
@annie519authorJul 24.2003 — 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!!!
×

Success!

Help @annie519 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...