Current location: Hot Scripts Forums » General Web Coding » JavaScript » binary in javascript


binary in javascript

Reply
  #1 (permalink)  
Old 02-07-05, 01:43 PM
bogomil bogomil is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Sofia, Bulgaria
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
binary in javascript

<script language="javascript">

for (x = 0; x < 127; x++) {

document.write("Decimal: ", x.toString(10), " Binary: ",
x.toString(2), "<BR>")
}
</script>


Result is:
Decimal: 0 Binary: 0
Decimal: 1 Binary: 1
Decimal: 2 Binary: 10
Decimal: 3 Binary: 11
Decimal: 4 Binary: 100

How could make the result with 5 signs for example as:

Decimal: 0 Binary: 00000
Decimal: 1 Binary: 00001

and so on
__________________
../more beer
Reply With Quote
  #2 (permalink)  
Old 02-09-05, 11:02 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
document.write("Decimal: "+x.toString(10)+" Binary: "+"00000".substring(0,5-x.toString(2).length)+x.toString(2)+"<BR>")
This will only show 5 numbers if x<32. If x>=32 there will be 6 or more binary digits.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible 2 stop a javascript function w/ other jv command? noni JavaScript 1 01-19-05 11:48 AM
javascript /forms /checkboxes /arrays ski_woman JavaScript 1 11-16-04 04:08 AM
javascript multiple select menu for php? isaacmlee JavaScript 1 10-15-04 09:53 AM
Order of vbscript and javascript in ASP marlin ASP 0 06-03-04 03:01 PM
Reaaly stuck about javascript over frames muratisik JavaScript 1 12-14-03 11:58 AM


All times are GMT -5. The time now is 10:42 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.