Change binary number ex. 1=0, 0=1

Hello everyone this is my first posting and I need some help I' trying to convert binary number let say If have 1 convert to 0 and viceversa..
this my code:

I am using Microsoft Visual studio 2010 ..and newbie in program.. I will really apreciate your help..
/*
assignTest.cpp


*/

#include<stdio.h>
#include<stdlib.h>
#include "Assign1.h"

using namespace std;

int main()
{
char record[500];
int x=0;
int total=0;
int ascii;
//char record1[500];
ifstream Infile ("input.txt");
if (Infile.fail())
{
cout <<"File Not Found";
exit (1);
}
else
{
while (!Infile.eof())
{
Infile.getline(record,sizeof(record));
cout<<record<<endl;

//Start to convert charater to ascii
int ConvertAscii(int num);
int num1=strlen(record);

for (x=0;x<num1; x++)
cout <<int(record[x]);
cout<<endl;
// cout << "Proxima";

// Convert Ascii to Binary

void ToBinary(char* record) ;
{
char * temprecord;
int k = 0;
temprecord = new char[500];
while (record[k] != '\0')
{

itoa((char)record[k], temprecord, 2);
k++;
cout << temprecord;
//
// cout<<endl;
}
cout<<endl;
//delete[] temprecord;
// Complement...
int i;

// cout<<record[k];

void ChangeNumber(char* temprecord);
{
char * temprecord1;
int k=0;
temprecord1= new char;
while (temprecord[k]!='\0')
{
if (temprecord[k]=='1')
temprecord1[k]='0';
else
{
temprecord1[k]='1';
}

}
cout <<"Print Invert Number";
cout <<temprecord1[k];
}

}
//for (i=0; i< k; i++)
//{
// if (temprecord[i]=='1')
//
// temprecord[i]='0';
//
// else
// {
// temprecord[i]='1';
// }
////char record1=record[i];
//
//
//
//}
//
////cout<<"complement values="<<record[i];
//
//cout <<"Prueba";
//cout << char(temprecord[i]);
//
//cout<<endl;


//}


}


}



}



Convert a number to and from what? What kind of number?
Last edited on
I trying to convert binary number ...let say I have a arrangement:

I have this binary number 100101 and where you have 1 to put 0 and viceversa ...

100101=011010
this all that I need
Are you storing the value as a string? If so, iterate through the string, and when you encounter 0, set it to 1 (ASCII: Oct. 49), but if it's 1, set it to 0 (ASCII: Oct. 48). For instance:

1
2
3
4
5
6
7
8
9
10
11
12
char String[] = "0101";
char *Itor(String);

while(*Itor)
{
    if(*Itor == '0')
       *Itor = '1';

    else *Itor = '0';

    ++Itor;
}


Wazzak
Last edited on
Also if you storing your number as an integer, then you can just use the ~ unrary complement operator.
http://www.cplusplus.com/doc/tutorial/operators/
Thanks a lot ....
Topic archived. No new replies allowed.