Help with Javascript code

C

Cogito

I'm playing around with code to convert numbers from decimal to
hexadecimal and vice versa. I was able to get the 'decimal to hex'
part going. But, I have no idea how to do the hex to decimal part.

Can you show me in the following sample what needs to be added?


<html>
<head>

<script language="JavaScript">
<!--
function HexCalc(op) {
var a;
var n1 = eval(document.CalcForm.num1.value);
if (op == "tohex") a = n1.toString(16).toUpperCase();
else if (op == "todec") a = no_idea_how_to_do_it;
document.CalcForm.answer1.value = a;
return;
}
//-->
</script>

</head>

<body>
<h1>Hex Calculator</h1>

<form name="CalcForm">
Dec:
<input type="text" name="num1" value="" size=10>
&nbsp;&nbsp;Hex:
<input type="text" name="answer1" value="" size=10>
<p>
<input type="button" value="to Hex" OnClick="HexCalc('tohex')">

<br><br><br><br>
Hex:
<input type="text" name="num2" value="" size=10>
&nbsp;&nbsp;Dec:
<input type="text" name="answer2" value="" size=10>
<p>
<input type="button" value="to Dec" OnClick="HexCalc('todec')">
</form>

</body>
</html>



Steve
 
R

Richard

Cogito said:
I'm playing around with code to convert numbers from decimal to
hexadecimal and vice versa. I was able to get the 'decimal to hex'
part going. But, I have no idea how to do the hex to decimal part.

I fail to understand why this is posted in a group which has nothing to do
with javascript.
Obviously, you would need at least one variable that determines whether or
not the character input is numerical or alpha. If it is alpha, assign it a
unique value.
 
R

rf

Richard said:
I fail to understand why this is posted in a group which has nothing to do
with javascript.

The last the the faq for this group was published it said quite clearly that
"almost anything web related goes".
Obviously, you would need at least one variable that determines whether or
not the character input is numerical or alpha. If it is alpha, assign it a
unique value.

Could you please explain exactly why "a variable that determines whether or
not the character input is numerical or alpha" would be required to convert
a string from hex to decimal? And what is this thing about a unique value?
 
R

rf

Cogito said:
I'm playing around with code to convert numbers from decimal to
hexadecimal and vice versa. I was able to get the 'decimal to hex'
part going. But, I have no idea how to do the hex to decimal part.

Can you show me in the following sample what needs to be added?
else if (op == "todec") a = no_idea_how_to_do_it;

a = parseInt(n1,16).toString(10);
 
C

Cogito

a = parseInt(n1,16).toString(10);

Thanks for your reply. I tried it but does not seem to give a correct
results. For some reason, regardless of my input number it produces
the result 'NaN'.

In order not to antagonise others you may wish to reply to
(e-mail address removed)
 
R

rf

Cogito said:
Thanks for your reply. I tried it but does not seem to give a correct
results. For some reason, regardless of my input number it produces
the result 'NaN'.

Stick an alert in for n1. It may not contain a valid hex number.
 
R

rf

Cogito said:
Thanks for your reply. I tried it but does not seem to give a correct
results. For some reason, regardless of my input number it produces
the result 'NaN'.

Replace
var n1 = eval(document.CalcForm.num1.value);
with
var n1 = document.CalcForm.num1.value;

You very rarely need to use eval and in this case it is actaully in error.
 
R

Richard

rf said:
a = parseInt(n1,16).toString(10);


Your saying here "parse integer in base 16 to a string in base 10".
How does this convert the value "AC" in hex to it's base 10 equivelant?
An integer is not an alpha character.
What if the user enters in "x" then what?
 
R

rf


The correct grammar is "You're"
saying here "parse integer in base 16 to a string in base 10".

Oh my. I'm saying no such thing.

<lesson type="javascript" level="101">
Read The Fucking Manual.
</lesson>

I am saying "parse the *string* in var n1 to an integer, using radix 16 and
then convert said integer back to a string, using radix 10.
How does this convert the value "AC" in hex to it's base 10 equivelant?

You mean equivalent.

parseInt(n1,16) converts the string "AC" (assuming said string is in var n1)
into the integer 172. The .toString method of said integer object converts
that integer into the string "172" and returns that string.
An integer is not an alpha character.

What on earth do you mean by this statement. The syntax is correct but the
semantics just do not scan. In any case you are wrong. The integer 49 can
quite readily be construed to be the character '1'. In fact this happens
every time you type a '1' into your computer. It is stored internally as the
integer 49 or (only if you speak C++ hex) 0x31.
What if the user enters in "x" then what?

"x" is not an hexadecimal digit so the parseInt will stop at that point.

Did you actually "try" this? No, I thought not.
 
C

Cogito

"x" is not an hexadecimal digit so the parseInt will stop at that point.

Hmmm, that's right. Well, if someone is willing to post a line or two
of code that handles non numerical (decimal or hex) digits it would be
much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top