Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   HTML (http://www.velocityreviews.com/forums/f31-html.html)
-   -   Help with Javascript code (http://www.velocityreviews.com/forums/t158585-help-with-javascript-code.html)

Cogito 07-03-2004 03:32 AM

Help with Javascript code
 
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

Richard 07-03-2004 04:23 AM

Re: Help with Javascript code
 

"Cogito" <nospam@nospam.nospam> wrote in message
news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com...
> 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.



rf 07-03-2004 05:36 AM

Re: Help with Javascript code
 

"Richard" <Anonymous@127.001> wrote in message
news:cc5cb20qkc@news4.newsguy.com...
>
> "Cogito" <nospam@nospam.nospam> wrote in message
> news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com...
> > 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.


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?

--
Cheers
Richard.



rf 07-03-2004 05:52 AM

Re: Help with Javascript code
 

"Cogito" <nospam@nospam.nospam> wrote in message
news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com...
> 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);



Cogito 07-03-2004 06:47 AM

Re: Help with Javascript code
 
On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <rf@example.com> wrote:

>
>"Cogito" <nospam@nospam.nospam> wrote in message
>news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com.. .
>> 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);
>


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
quarkcheese@hotmail.com



rf 07-03-2004 07:28 AM

Re: Help with Javascript code
 

"Cogito" <nospam@nospam.nospam> wrote in message
news:3blce0tn15gjodt219gt9vqpg2v746lrr7@4ax.com...
> On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <rf@example.com> wrote:
>
> >
> >"Cogito" <nospam@nospam.nospam> wrote in message
> >news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com.. .
> >> 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);
> >

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

--
Cheers
Richard.



rf 07-03-2004 07:32 AM

Re: Help with Javascript code
 

"Cogito" <nospam@nospam.nospam> wrote in message
news:3blce0tn15gjodt219gt9vqpg2v746lrr7@4ax.com...
> On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <rf@example.com> wrote:
>
> >
> >"Cogito" <nospam@nospam.nospam> wrote in message
> >news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com.. .
> >> 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);
> >

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

> In order not to antagonise others you may wish to reply to
> quarkcheese@hotmail.com
>
>




Cogito 07-03-2004 10:12 AM

Re: Help with Javascript code
 
>Replace
>var n1 = eval(document.CalcForm.num1.value);
>with
>var n1 = document.CalcForm.num1.value;



It's all working fine now. Thank you very much.

Steve

Richard 07-03-2004 01:50 PM

Re: Help with Javascript code
 

"rf" <rf@example.com> wrote in message
news:fQrFc.76252$sj4.14134@news-server.bigpond.net.au...
>
> "Cogito" <nospam@nospam.nospam> wrote in message
> news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com...
> > 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);



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?



rf 07-03-2004 03:12 PM

Re: Help with Javascript code
 

"Richard" <Anonymous@127.001> wrote in message
news:cc6die0t8t@news1.newsguy.com...
>
> "rf" <rf@example.com> wrote in message
> news:fQrFc.76252$sj4.14134@news-server.bigpond.net.au...
> >
> > "Cogito" <nospam@nospam.nospam> wrote in message
> > news:n4ace0le19r5modnfe50qlkauae40b8jch@4ax.com...
> > > 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);


> Your


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 ****ing 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.

--
Cheers
Richard.




All times are GMT. The time now is 02:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.