Help with Javascript code

Discussion in 'HTML' started by Cogito, Jul 3, 2004.

  1. Cogito

    Cogito Guest

    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
     
    Cogito, Jul 3, 2004
    #1
    1. Advertising

  2. Cogito

    Richard Guest

    "Cogito" <> wrote in message
    news:...
    > 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.
     
    Richard, Jul 3, 2004
    #2
    1. Advertising

  3. Cogito

    rf Guest

    "Richard" <Anonymous@127.001> wrote in message
    news:...
    >
    > "Cogito" <> wrote in message
    > news:...
    > > 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, Jul 3, 2004
    #3
  4. Cogito

    rf Guest

    "Cogito" <> wrote in message
    news:...
    > 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);
     
    rf, Jul 3, 2004
    #4
  5. Cogito

    Cogito Guest

    On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <> wrote:

    >
    >"Cogito" <> wrote in message
    >news:...
    >> 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
     
    Cogito, Jul 3, 2004
    #5
  6. Cogito

    rf Guest

    "Cogito" <> wrote in message
    news:...
    > On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <> wrote:
    >
    > >
    > >"Cogito" <> wrote in message
    > >news:...
    > >> 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, Jul 3, 2004
    #6
  7. Cogito

    rf Guest

    "Cogito" <> wrote in message
    news:...
    > On Sat, 03 Jul 2004 05:52:11 GMT, "rf" <> wrote:
    >
    > >
    > >"Cogito" <> wrote in message
    > >news:...
    > >> 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
    >
    >
    >
     
    rf, Jul 3, 2004
    #7
  8. Cogito

    Cogito Guest

    >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
     
    Cogito, Jul 3, 2004
    #8
  9. Cogito

    Richard Guest

    "rf" <> wrote in message
    news:fQrFc.76252$...
    >
    > "Cogito" <> wrote in message
    > news:...
    > > 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?
     
    Richard, Jul 3, 2004
    #9
  10. Cogito

    rf Guest

    "Richard" <Anonymous@127.001> wrote in message
    news:...
    >
    > "rf" <> wrote in message
    > news:fQrFc.76252$...
    > >
    > > "Cogito" <> wrote in message
    > > news:...
    > > > 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 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.

    --
    Cheers
    Richard.
     
    rf, Jul 3, 2004
    #10
  11. Cogito

    Cogito Guest

    On Sat, 03 Jul 2004 15:12:34 GMT, "rf" <> wrote:

    >> What if the user enters in "x" then what?

    >
    >"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.
     
    Cogito, Jul 4, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,245
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. nick
    Replies:
    3
    Views:
    3,201
    Eliyahu Goldin
    Dec 15, 2004
  3. laxmilal Menaria via DotNetMonster.com

    Use aspx.vb Code to Javascript Code

    laxmilal Menaria via DotNetMonster.com, May 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,745
    laxmilal Menaria via DotNetMonster.com
    May 13, 2005
  4. Arthur Connor
    Replies:
    0
    Views:
    402
    Arthur Connor
    Dec 30, 2003
  5. Dr John Stockton

    How to outsource Javascript code from html code ?

    Dr John Stockton, Dec 31, 2003, in forum: Javascript
    Replies:
    0
    Views:
    115
    Dr John Stockton
    Dec 31, 2003
Loading...

Share This Page