So in this method im trying to make a decimal to binary method that I can use later. when ever I enter a number for a I get some weird stuff for an output. like if I entered 6 for a, I get 2686808-192466809244744141985802686...."blah blah blah"....77024110
#include <iostream>
usingnamespace std;
void binary(int n)
{
int binaryArray [8];
int tempBinary;
for(int i = 0; n != 0; i++)
{
tempBinary = n%2;
binaryArray[i] = tempBinary;
n = n/2;
}
for(int i = sizeof(binaryArray)-1; i >= 0; i--){
cout << binaryArray[i];
}
}
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Please enter a value for 'A' and a value for 'B'";
int a;
int b;
int n;
cin >> a >> b;
n = a;
binary(n);
}
And you got errors because you don't initialize the array completely. It is printing rubbish, I guess you entered 6 right? Look t the last 3 numbers, 110 is binary 6.
Why not use bit shifting:
1 2 3 4 5 6 7 8 9 10
for(int i=0;i<8;i++)
{
binaryArray[i] = n & 0x01;
n = n >> 1;
}
for(int i=7;i>=0;i++)
{
cout << binaryArray[i];
}
#include <iostream>
usingnamespace std;
void binary(int n)
{
int binaryArray [8];
int tempBinary;
for(int i = 0; n != 0; i++)
{
tempBinary = n%2;
binaryArray[i] = tempBinary;
n = n/2;
}
for(int i = sizeof(binaryArray)-1; i >= 0; i--){
cout << binaryArray[i];
}
}
int main(int nNumberofArgs, char* pszArgs[])
{
cout << "Please enter a value for 'A' and a value for 'B'";
int a;
int b;
int n;
cin >> a >> b;
n = a;
binary(n);
}
Look at line 15.
sizeof(binaryArray) is much larger than the number of elements in the array.
sizeof(binaryArray)/sizeof(binaryArray[0]) is correct.
Also if you make line 7:
int binaryArray [sizeof(int)*8] = {0};
There will be enough storage no matter what the value passed in is (making the assumption that bytes are 8 bits -- they aren't on all platforms) and all of the elements will be initialized to 0.
It is because you use sizeof... Sizeof gets the size of the array in bytes and your array is made of ints which are more than 1 byte each so it will result in a larger number than the 8 you expect... 8*sizeof(int)
#include <stdio.h>
#include<conio.h>
int main()
{
printf("Binary value of 1234 is: %s\r\n", itob(1234);
getch();
}
// Integer to binary ascii string
char *itob(uint uiInt)
{
char szStr[33];
uint uiTemp;
uchar ucPtr = 0;
uchar ucStart = 0;
uint uiDivider = 0x8000;
// Because integer can be (32bit) or (16bit) then test for it.
// Compiler depended.
if (sizeof(uiInt) > 2) {
uiDivider = 0x80000000;
}
// Do all bits and start making string on first '1' binary
do {
uiTemp = (uiInt / uiDivider);
if (uiTemp == 1) {
uiInt -= uiDivider;
// Set flag for first '1' to do string
ucStart = 1;
}
uiDivider /= 2;
// Make ascii
szStr[ucPtr] = (char)(uiTemp + '0');
// If result have been a '1' then increment pointer
if (ucStart == 1) {
ucPtr++;
}
// Repeat until last bit
} while (uiDivider >= 2);
// Make ascii
szStr[ucPtr++] = (char)(uiInt + '0');
// End string with NULL
szStr[ucPtr] = 0;
return szStr;
}