OZONE Asylum
Forums
DHTML/Javascript
Decimal to Binary Conversion
This page's ID:
28070
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
[quickie] there's a typo in the declaration of [i]resultstring[/i] there's one too many [i]) [/i] in the first [i]if( originalNumber ... [/i] there's one too too many [i]([/i] and one too few [i])[/i] few in the next [i]if( originalNumber ... [/i] :rolleyes: sorry but are you sure you wrote/read that code carefully ?! Anyway, base conversions are better done with the built-in functions ( [url=http://docs.sun.com/source/816-6408-10/toplev.htm#1064173]parseInt[/url] and [url=http://docs.sun.com/source/816-6408-10/number.htm#1193464]toString[/url] ), i.e :[code]function ConvertBinary() { if (document.form1.dropdown.value == "dectobin") { var srcBase = 10, dstBase = 2 } else { var srcBase = 2, dstBase = 10 } var originalValue = document.form1.original1.value.replace( /^\s+|\s+$/, '' ), originalNumber = parseInt( originalValue, srcBase ); if( isNaN(originalNumber) ) // /!\ that safety test should be improved var resultString = 'invalid input' else var resultString = originalNumber.toString( dstBase ) document.form1.result1.value= resultString; }[/code] And your HTML markup could be improved too :p The [i]language[/i] attribute and CENTER tag are deprecated. You could/should use [i]label[/i] tags around your FORM fields. Oh, and adding a DOCTYPE would be nice too. Hope that helps, [/quickie] [url=http://www.p01.org/][sigrotate][img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_love.gif[/img]|[img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_love_small.gif[/img]|[img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_charly.gif[/img]|[img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_dk.gif[/img]|[img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_reason.gif[/img]|[img]http://poi.ribbon.free.fr/files/p01_ozoneasylum_sig_galaxy.gif[/img][/sigrotate][/url] [small](Edited by [url=http://www.ozoneasylum.com/user/2185]poi[/url] on 06-13-2006 10:18)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »