Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-04-2009, 06:01 PM   PM User | #1
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
convert binary to decimal an vise versa

hi..

need convert decimal to binary and viser versa using on change attribute ...
can someone help me
kasu is offline   Reply With Quote
Old 11-04-2009, 06:24 PM   PM User | #2
Philip M
Master Coder

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 7,092
Thanks: 85
Thanked 836 Times in 817 Posts
Philip M will become famous soon enoughPhilip M will become famous soon enough
Google is your friend!

http://www.javascriptkit.com/script/...nconvert.shtml

http://javascript.internet.com/misce...o-decimal.html

It is your responsibility to die() if necessary….. - PHP Manual

Last edited by Philip M; 11-04-2009 at 06:27 PM..
Philip M is offline   Reply With Quote
Old 11-04-2009, 07:21 PM   PM User | #3
jmrker
Regular Coder

 
Join Date: Aug 2006
Location: FL
Posts: 655
Thanks: 1
Thanked 121 Times in 118 Posts
jmrker is on a distinguished road
Unhappy Too slow ...

Alternative code, just because I working and am a slow typist.

Code:
<html>
<head>
<title>Binary Conversion</title>

<script type="text/javascript">
// For: http://codingforums.com/showthread.php?t=181347

function Dec_Bin(val) { return parseInt(val,10).toString(2); }
function Bin_Dec(val) { return parseInt(val,2).toString(10); }
</script>

</head>
<body>
Binary: <input type="text" id="bin" value='1010'>
<button
 onclick="document.getElementById('dec').value=Bin_Dec(document.getElementById('bin').value)"> Bin to Dec</button>
<button onclick="document.getElementById('bin').value = ''">Clear</button>

<br>
Decimal: <input type="text" id="dec" value=''>
<button
 onclick="document.getElementById('bin').value=Dec_Bin(document.getElementById('dec').value)"> Dec to Bin</button>
<button onclick="document.getElementById('dec').value = ''">Clear</button>
</body>
</html>
Can make similar functions for HEX and OCT conversions.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
kasu (11-05-2009)
Old 11-04-2009, 11:16 PM   PM User | #4
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
Thanks a lot for the reply..but the problem I have is how do you do it using "onchange" attribute rather than using buttons..and converting either decimal or binary to a hexadecimal...working all this time but did not get it ryt!!

Last edited by kasu; 11-04-2009 at 11:19 PM..
kasu is offline   Reply With Quote
Old 11-05-2009, 12:21 AM   PM User | #5
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
The only things important to know are that you can convert text to number using any radix (base) using:
Code:
var num = parseInt( text, radix );
So you read binary with a radix of 2, decimal with a radix of 10, hex with a radix of 16.

To convert *from* a number to text in the proper base/radix, you just put the radix in the toString():
Code:
var text = num.toString( radix );
And whether you use a button or onchange or onblur or whatever has no impact on those functions and their results.
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
kasu (11-05-2009)
Old 11-05-2009, 02:49 AM   PM User | #6
jmrker
Regular Coder

 
Join Date: Aug 2006
Location: FL
Posts: 655
Thanks: 1
Thanked 121 Times in 118 Posts
jmrker is on a distinguished road
Question

Quote:
Originally Posted by kasu View Post
hi..

need convert decimal to binary and viser versa using on change attribute ...
can someone help me
Show what you want to do so we can make a recommendation as to how to do it.
jmrker is offline   Reply With Quote
Old 11-05-2009, 04:02 AM   PM User | #7
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
need to have 2 text boxes ..one to enter the decimal..an the other displays the corresponding binary value and viser versa..
kasu is offline   Reply With Quote
Old 11-05-2009, 06:58 AM   PM User | #8
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
Code:
<form>
Enter decimal: 
    <input name="decimal" onchange="this.form.binary.value=(parseInt(this.value,10)).toString(2);">
<hr>
Enter binary:
    <input name="binary" onchange="this.form.decimal.value=(parseInt(this.value,2)).toString(10);">
</form>
This really is the same code jmrker showed you, you know. Only the way it is "triggered" is different. PLEASE don't tell us this is homework for some class.
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
kasu (11-05-2009)
Old 11-09-2009, 03:04 PM   PM User | #9
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
how do you perform this coding with a for loop..
<html>


<head>
<title> Dice Stats </title>
<script type="text/javascript"
src="http://random.js">
</script>
<script type="text/javascript">
function RollRepeatedly()
// Assumes: repsBox contains a number
// Results: simulates that many dice rolls, displays # of doubles
{
var doubleCount, totalRolls, repCount, roll1, roll2;

doubleCount = 0; // INITIALIZE THE COUNTER

totalRolls = document.getElementById("repsBox").value;
totalRolls = parseFloat(totalRolls);

repCount = 0;
while (repCount < totalRolls) { // REPEATEDLY:
roll1 = RandomInt(1, 6); // SIMULATE THE DICE ROLLS
roll2 = RandomInt(1, 6);

if (roll1 == roll2) { // IF DOUBLES,
doubleCount = doubleCount + 1; // INCREMENT THE COUNTER
}

repCount = repCount + 1; // INCR. REPETITION COUNTER
}

document.getElementById("countBox").value = doubleCount; // display #
}
</script>

</head>

<body>
<div style="text-align:center">
<h2>Dice Stats</h2>
<p>
Desired number of rolls =
<input type="text" id="repsBox" size="6" value="1000" />
</p>

<p>
<input type="button" value="Click to Roll" onclick="RollRepeatedly();" />
</p>
<p>
Number of doubles obtained =
<input type="text" id="countBox" size="6" value="0" />
</p>
</div>
</body>
</html>

Last edited by kasu; 11-09-2009 at 10:40 PM..
kasu is offline   Reply With Quote
Old 11-09-2009, 03:47 PM   PM User | #10
jmrker
Regular Coder

 
Join Date: Aug 2006
Location: FL
Posts: 655
Thanks: 1
Thanked 121 Times in 118 Posts
jmrker is on a distinguished road
Arrow

Beginning to look like a homework assignment ...
See: http://www.webdeveloper.com/forum/sh...d.php?t=219590
jmrker is offline   Reply With Quote
Old 11-09-2009, 04:15 PM   PM User | #11
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,066
Thanks: 30
Thanked 143 Times in 139 Posts
Kor will become famous soon enough
Quote:
Originally Posted by kasu View Post
how do you perform this coding with a for loop..
I guess Old Pedant already gave you all the information you need.

To convert a value from a base to decimal:
Code:

parseInt(value,base)
To convert a value from decimal to a base:
Code:
value.toString(base)
The way you could perform that within a loop is a different thing, and I guess you should open another thread or, hm... that should be an easy task even for a beginner.
__________________
KOR
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 11-09-2009 at 04:18 PM..
Kor is offline   Reply With Quote
Old 11-19-2009, 04:02 AM   PM User | #12
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
hi wondering a start to this ...just give me a idea plzz to create the following
*
***
*****
*******
*****
***
*

*
* *
* *
* *
* *
* *
*

the above diagram is displayed incorrectly on the thread..plzz refer the page source of this page LINE 3105 COlUMN 3 to get the correct picture

Last edited by kasu; 11-19-2009 at 04:23 AM..
kasu is offline   Reply With Quote
Old 11-19-2009, 07:09 AM   PM User | #13
Old Pedant
Senior Coder

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Location: Snohomish, WA
Posts: 4,061
Thanks: 18
Thanked 660 Times in 652 Posts
Old Pedant will become famous soon enoughOld Pedant will become famous soon enough
LOL! So you and Dartz101 are in the same class:

http://www.codingforums.com/showthread.php?t=182173

Tell me, which of you is getting the better grade?

And, I also answered this last question from yet another student in your same class:
http://www.codingforums.com/showthread.php?t=182091

He seems to be doing better than either you or Dartz101, no?

Where the heck is this class, anyway??
__________________
"Old age and cunning win out over youth and enthusiasm every time."
Old Pedant is offline   Reply With Quote
Old 11-19-2009, 09:38 AM   PM User | #14
kasu
New to the CF scene

 
Join Date: Nov 2009
Location: SL
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
kasu is an unknown quantity at this point
i have no idea as to who is dartz101...and i checked the link uve posted but it does not have any relation to what my question is..and the second link aint workin..... I just wanted to create a diamond using asterix ....and i hope some one give me an idea to start off..i dont want any of u 2 do my homework..but hope a help from some one ..that will be great coz am a beginner..
kasu is offline   Reply With Quote
Old 11-19-2009, 10:06 AM   PM User | #15
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 6,066
Thanks: 30
Thanked 143 Times in 139 Posts
Kor will become famous soon enough
Quote:
Originally Posted by kasu View Post
i have no idea as to who is dartz101...and i checked the link uve posted but it does not have any relation to what my question is..and the second link aint workin..... I just wanted to create a diamond using asterix ....and i hope some one give me an idea to start off..i dont want any of u 2 do my homework..but hope a help from some one ..that will be great coz am a beginner..
1. This should be a different problem, so stop this then go and open another thread.
2. We hardly wait to see what you have done so far - your code I mean - in order to help you.
__________________
KOR
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:14 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.