Signed Binary to Decimal/Signed Decimal to Binary

Hello.

I currently have functions for signed decimal to binary and signed binary to decimal. The code works fine if you enter a positive integer. However it isn't giving out the correct answers when a negative number is inputted.

For example when I input -1 to convert to binary, I will get -1. But the correct answer is 1111 1111. If I were to input 1111 1111 to convert to decimal, I will get 255 instead of -1.

I have tried two's compliment in my code, but it just seems to change whether or not the answer is positive/negative. (ie input 7 will give me -111). I assume it's needed in some way though.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

int SignedDecimalToBinary()
{
    long decimal = 1; 
    long remainder = 1;
    long i = 1;
    long sum = 0;

    cout << "Enter Decimal Number: \n" << endl;
    cout << "Decimal: "<< endl;
    cin >> decimal;

    do
    {
        //Conversion to Signed Binary.
        remainder = decimal %2;
        sum = sum + (i * remainder);
        decimal = decimal / 2;
        i = i * 10;
    }while (decimal < 0 || decimal > 0); //Less Than 0 to Make Value Signed.

    cout << "Binary: " << sum << endl << endl;

    return 0;
}

int binary_decimal(int n) //Function For Signed Binary to Decimal.
{
    int decimal = 0, i = 0, remainder;

    //Two's Compliment.
    //n = ~n + 1;

    //Conversion to Decimal.
    while (n != 0)
    {
        remainder = n % 10;
        n /= 10;
        decimal += remainder * pow(2,i);
        ++i;
    }
    return decimal;
}

int SignedBinaryToDecimal()
{
    int binary_decimal(int n);

    int n;

    cout << "Enter Binary Number: ";
    cin >> n;
	
    cout << "In Decimal: " << n << " =  " << binary_decimal(n) << endl;
	
    return 0;
}

int main()
{
    SignedDecimalToBinary();
    SignedBinaryToDecimal();

    system("pause");
    return 0;
}



Regards

Inek
I only got as far as trying a single positive decimal, 1234 which gave the binary result of 1421075418, to see that there is something deeply flawed in your approach. That and the use of the pow() function, which is a very heavy-duty solution to a relatively lightweight problem.

My apologies, I've not attempted to debug your code. However, I offer half an answer, a decimal to binary function which works with either positive or negative decimal numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

string DecimalToBinary(unsigned int dec)
{
    char binary[33] = {0}; 
    
    int ix = 32; // subscript of current character
    do 
    {
        binary[--ix] = '0' + dec % 2;
        dec /= 2;
    } while (dec);
    
    return  (binary+ix);    // ix is offset into char array where answer begins
}

int main()
{
    int decimal;
    
    cout << "Enter Decimal Number: ";
    cin >> decimal;
    cout << "Binary: " << DecimalToBinary(decimal);


    return 0;
}
Thank you, much appreciated. Works lovely.

I think the issue with my binary to decimal function is that it still thinks it's unsigned. ie: For 1111 1111 I get 255. While that is correct, the output needs to be -1.

EDIT: Nvm worked out the other function out. Just needed to add decimal = (decimal + 128)%256 - 128; after the while loop.
Last edited on
Topic archived. No new replies allowed.