<html>
<head>
<script>
"use strict";
function hexidecimal() {
	var num = document.getElementsByName('number')[0].value;
	var hex = num.toString(16);
alert(hex);
alert(num.toString(2));
}
</script>
</head>
<body>
<form id="form">
<input type="text" name="number" />
<input type="button" value="Hex" onClick="hexidecimal();" />
</form>
</body>
</html>

Above is the current code I have, I expect it to output the contents of whatever is held in the number input box as a hexidecimal number, instead it outputs whatever number is put into the box as base 1. Can anyone shed some light on what I have done wrong please?

Thanks

Recommended Answers

All 2 Replies

Convert your num (string) to an int first, using parseInt:

var hex = parseInt(num).toString(16);

Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.