New topic: 

How can I show leading 0's in a binary number?

<http://forums.realsoftware.com/viewtopic.php?t=47426>

         Page 1 of 1
   [ 4 posts ]                 Previous topic | Next topic          Author  
Message        cbmeeks          Post subject: How can I show leading 0's in a 
binary number?Posted: Wed Mar 27, 2013 7:42 pm                         
Joined: Tue Mar 19, 2013 7:11 am
Posts: 8                I have a label where I am displaying the byte value of 
a variable in binary format. It works fine but I want to make sure I show all 8 
bits. 

Currently, if the value is 1 then all I see is "1" when I should see 
"00000001". 127 works just fine as it shows "10000000".

Here is what I am using:

Str( Bin( App.Sprites(spriteCurrent).Data.Byte(b) ) )

Any help would be appreciated.

Thanks!   
                             Top                cbmeeks          Post subject: 
Re: How can I show leading 0's in a binary number?Posted: Wed Mar 27, 2013 8:09 
pm                         
Joined: Tue Mar 19, 2013 7:11 am
Posts: 8                I was able to do it this way but it seems kludgey.

labSpriteEditByteValue.Text = Right( "00000000" + Str( Bin( 
App.Sprites(spriteCurrent).Data.Byte(b) ) ), 8 )

Anything better?   
                             Top                bob_barr          Post subject: 
Re: How can I show leading 0's in a binary number?Posted: Wed Mar 27, 2013 8:17 
pm                         
Joined: Sat Mar 25, 2006 1:39 pm
Posts: 894
Location: Morgan Hill, CA                You should take a look at the Format() 
function. Unlike Str(), that function allows you to specify the format in which 
the number will be displayed.

[added]
Sorry, my mistake. See Dave's post below this one.      
_________________
While fairly new to RB, I've been programming ever since that darned mastadon 
stepped on my abacus. Using RB 2009r5.1 on XP and Windows 7.      

    Last edited by bob_barr on Wed Mar 27, 2013 10:03 pm, edited 1 time in 
total.   
                             Top                DaveS          Post subject: 
Re: How can I show leading 0's in a binary number?Posted: Wed Mar 27, 2013 9:44 
pm                                 
Joined: Sun Aug 05, 2007 10:46 am
Posts: 4707
Location: San Diego, CA                You can leave the STR( function out... 
BIN( already returns a STRING
and FORMAT doesn't format non-decimal numbers.... that is what BIN(, HEX( and 
OCT( are for

labSpriteEditByteValue.Text = Right( "00000000" + Bin( 
App.Sprites(spriteCurrent).Data.Byte(b) ), 8 )      
_________________
Dave Sisemore
MacPro, OSX Lion 10.7.4 RB2012r1
Note : I am not  interested in any solutions that involve custom Plug-ins of 
any kind  
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 4 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to