Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,582 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,918 people online right now. Registration is fast and FREE... Join Now!




32 bit binary number to decimal

 
Reply to this topicStart new topic

32 bit binary number to decimal

andre03
5 Oct, 2008 - 05:02 PM
Post #1

New D.I.C Head
*

Joined: 5 Oct, 2008
Posts: 1

convert a 32 bit binary number to a decimal number


PLEASE HELP !!!!!!!!
User is offlineProfile CardPM
+Quote Post

David W
RE: 32 Bit Binary Number To Decimal
5 Oct, 2008 - 06:25 PM
Post #2

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
QUOTE(andre03 @ 5 Oct, 2008 - 06:02 PM) *

convert a 32 bit binary number to a decimal number

PLEASE HELP !!!!!!!!


Here is one way ... done in HLA ...

CODE
program bitStrToDec2;

#include( "stdlib.hhf" )

static
    s1:        string:=    "00001111000011110000111100001111";
    s2:        string:=    "11110000111100001111000011110000";
    s3:        string:=    "01111111111111111111111111111111";
    s4:        string:=    "11111111111111111111111111111111";    

// Returns the decimal value of bit string 's' in the EAX register    ...
#macro my_bitStrToDec( s );
    push( ebx );
    push( ecx );
    push( edx );
    
    mov( 0, eax );
    mov( s, ebx ); // get address of the first char of s into eax register ...
    mov( 1, edx );
    for( mov( 31, ecx ); (type int32 ecx) > 0; dec(ecx) ) do
        if( (type char [ebx+ecx]) == '1' ) then
            or( edx, eax );
        endif;
        shl( 1, edx );
    endfor;
    if( (type char [ebx+ecx]) == '1' ) then
            or( edx, eax );
    endif;
    
    pop( edx );
    pop( ecx );
    pop( ebx );
#endmacro
    
begin bitStrToDec2;
    
    // handle first evaluation ...
    my_bitStrToDec( s1 );
    stdout.put( "The 32 'bits' in string ", s1, " = ", (type uns32 eax), " decimal.", nl );    
    
    // handle 2nd evaluation ...
    my_bitStrToDec( s2 );
    stdout.put( "The 32 'bits' in string ", s2, " = ", (type uns32 eax), " decimal.", nl );    

    // handle 3rd evaluation ...
    my_bitStrToDec( s3 );
    stdout.put( "The 32 'bits' in string ", s3, " = ", (type uns32 eax), " decimal.", nl );
    
    // handle 4th evaluation ...
    my_bitStrToDec( s4 );
    stdout.put( "The 32 'bits' in string ", s4, " = ", (type uns32 eax), " decimal.", nl );    
    
    stdout.put( "Press 'Enter' key to continue ... " );
    stdin.readLn();
    
end bitStrToDec2;


Or here is an other slightly different way ... also in HLA (high level assembly) ...

CODE

program bitStrToDec;

#include( "stdlib.hhf" )

static
    s1:        string:=    "00001111000011110000111100001111";
    s2:        string:=    "11110000111100001111000011110000";
    s3:        string:=    "01111111111111111111111111111111";
    s4:        string:=    "11111111111111111111111111111111";    

// Returns the decimal value of bit string 's' in the EAX register    ...
#macro my_bitStrToDec( s );
    push( ebx );
    push( ecx );
    
    mov( 0, eax );
    mov( s, ebx ); // get address of the first char of s into eax register ...
    for( mov( 0, ecx ); ecx < 31; inc(ecx) ) do
        if( (type char [ebx+ecx]) == '1' ) then
            add( 1, eax );
        endif;
        shl( 1,eax );
    endfor;
    if( (type char [ebx+ecx]) == '1' ) then
            add( 1, eax );
    endif;
    
    pop( ecx );
    pop( ebx );
#endmacro
    
begin bitStrToDec;
    
    // handle first evaluation ...
    my_bitStrToDec( s1 );
    stdout.put( "The 32 'bits' in string ", s1, " = ", (type uns32 eax), " decimal.", nl );    
    
    // handle 2nd evaluation ...
    my_bitStrToDec( s2 );
    stdout.put( "The 32 'bits' in string ", s2, " = ", (type uns32 eax), " decimal.", nl );    

    // handle 3rd evaluation ...
    my_bitStrToDec( s3 );
    stdout.put( "The 32 'bits' in string ", s3, " = ", (type uns32 eax), " decimal.", nl );
    
    // handle 4th evaluation ...
    my_bitStrToDec( s4 );
    stdout.put( "The 32 'bits' in string ", s4, " = ", (type uns32 eax), " decimal.", nl );    
    
    stdout.put( "Press 'Enter' key to continue ... " );
    stdin.readLn();
    
end bitStrToDec;


This post has been edited by David W: 5 Oct, 2008 - 08:47 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:38AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month