Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 11th, 2004, 10:16 PM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default 2's Complement binary to decimal conversion

Can someone please help me work out how I can wite in C++, a function to convert a 2's complement binary number to its equivalent decimal number, which is in the range -128 to 127??

gbilios

George N. Bilios
__________________
gbilios
Reply With Quote
  #2 (permalink)  
Old March 12th, 2004, 12:30 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not sure what you mean. Do you mean that you want to extract the decimal digits from a number that is the value of a variable in C or C++? Or what?

printf() in C or C++ (or cout << in C++ ) does the job. However, if this is a programming exercise, maybe you could give us a clue as to what you really want to do.

Dave
Reply With Quote
  #3 (permalink)  
Old March 12th, 2004, 03:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sounds to me like you have a string representation of a binary number, e.g. "10010101", and you need to convert this to either an decimal integer (or a string representation of one).

Either way, it sounds like a homework assignment to me, so you should double check with your teacher and/or school to verify that it's okay for you to get help from these forums. Most schools/teachers restrict the places a student is allowed to get help for solving programming problems.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old March 13th, 2004, 06:34 AM
Authorized User
 
Join Date: Mar 2004
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
Default

This is what i want to do:
2's complement binary numbers are entered (exactly 8-bits) at the command line. Then
the decimal output will be between -128 to +127 rather than 0 to 255.




George N. Bilios
Reply With Quote
  #5 (permalink)  
Old February 5th, 2009, 02:33 PM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert 2's complement to decimal

If the number is positive then the binary representation is the number
eg 111 = 7, 101 = 5 etc ...
if the number is negative the most signigicant bit will be set to a one
if the word is a 16 bit integer then bit 15 will be set to 1 -

You need to flip all the bits that are one to 0 and all the bits that are 0 to 1's

eg: 1111 1111 1111 0110
flip 0000 0000 0000 1001 = 9

add 1
=10
and make it negative

-10

Or

1111 1111 1111 0110

Exclusive Or with

1111 1111 1111 1111

0000 0000 0000 1001

add 1

and make negative


Hope that helps!
Reply With Quote
  #6 (permalink)  
Old March 15th, 2009, 11:34 AM
Registered User
 
Join Date: Mar 2009
Posts: 7
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hope this helps
Code:
#include <iostream>
#include <cassert>
#include <iomanip>
#include <cstring>
using std::setw;
using std::cout;
using std::endl;

const int BITS = 8;

int convert(char binary[]) {
    int power = 128;
    int sum = 0;
    assert(strlen(binary) == 8);

    for ( int i=0; i<BITS; ++i) {
        if ( i==0 && binary[i]!='0') {
            sum = -128;
        }
        else {
            sum += (binary[i]-'0')*power;
        }
        power /= 2;
    }
    return sum;
}
int main()
{
    char bin1[BITS+1] = "00000011";
    cout <<"Binary: "<< bin1 <<endl<<"Dec:    "<< setw(8) << convert(bin1) << endl<< endl;
    
    char bin2[BITS+1] = "10000000";
    cout <<"Binary: "<< bin2 <<endl<<"Dec:    "<< setw(8) << convert(bin2) << endl<< endl;

    char bin3[BITS+1] = "01111111";
    cout <<"Binary: "<< bin3 <<endl<<"Dec:    "<< setw(8) << convert(bin3) << endl<< endl;

return 0
}
Reply With Quote
The Following User Says Thank You to bert For This Useful Post:
RustyOnRampage (February 25th, 2013)
  #7 (permalink)  
Old April 21st, 2010, 06:12 AM
Registered User
 
Join Date: Apr 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can you please tell me the code to convert decimal to 2's complement ,thanks
Reply With Quote
  #8 (permalink)  
Old February 25th, 2013, 09:46 AM
Registered User
 
Join Date: Feb 2013
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thnx a lot, but I,m really confused with the purpose of (binary[i]-'0') in the following statement sum =sum+ (binary[i]-'0')*power;.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
string to binary conversion in XSLT navi25584 XSLT 5 September 3rd, 2008 03:14 AM
PROGRAM FOR DECIMAL TO BINARY CONVERSION bharathidhas C# 1 December 13th, 2006 02:53 AM
hex conversion to binary files chrispbrown2255 C++ Programming 1 March 15th, 2006 06:06 PM
Decimal to Binary Rafter VB.NET 2002/2003 Basics 1 February 9th, 2006 10:23 AM
decimal string to binary se7ss C# 5 July 19th, 2004 02:52 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.