Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

suggest

Status
Not open for further replies.

kante

Programmer
Dec 15, 2000
7
IT
i have this function
// Print binary representation of ints
#include <iostream>
using namespace std;
void main() {
int n;
while (cin >> n) {
cout << &quot;decimal: &quot; << n << endl;
// print binary with leading zeros
cout << &quot;binary : &quot;;
for (int i=31; i>=0; i--) {
int bit = ((n >> i) & 1)
cout << bit;
}
cout << endl;
}//end loop
}

i need to modify this function in this way :
instead of display the result of the conversion on the screen i would like to make a function that accepts two parameters

Dec2Bin(int DecToConvert, int LenghtOfConversion)
and returns the binary number with the specified LengthOfConversion.
(eg an Array of char ,length: LengthOfConversion)

thank you
 
why not do this

Code:
void Dec2Bin(int val, int len,char* buffer)
{

 for(int i = len-1,j=0;j<len;i--,j++)
 {
   buffer[j] = ( (val & (1<<(i)) ? '1':'0'));
 }
	
}

some function

....

char buffer[33];
memset(buffer,0,33);
Dec2Bin(val,len,buffer);

cout<<buffer<<endl;

Matt
 
i tryed your function and it's not always good

try this
char buffer[33];
memset(buffer,0,33);
int val=5;
int len=50;
for(int i = len-1,j=0;j<len;i--,j++)
{
buffer[j] = ( (val & (1<<(i)) ? '1':'0'));
}
printf(&quot;%s&quot;,buffer);

it seems it returns a strange result with big len values
 
I didnt think you would want to output a buffer with infinite leading zeros. It was hard coded to show you the basics of how to output it. The size of an int is 32 bits (in most cases) and therefore, I assumed that you would not need more. I dont see why you would need to unless you were using int64 which would then change the buffer declaration from 33 to 65. If you want infinite leading zeros, then make the minor modification to the call where you declare buffer. Declare it dynamically as a new char[len+1] and handle it that way.

Matt
 
oh that's clear now!
i didn't understand why giving for example
val=5
len>32
it seemed that it was repeating a pattern.... of 32 elements...
'cause int is 32 byte encoded.... :S
33 cause it's 32 + '\0' :D
thank you...

is there a way, in your opinion to make the func
void Dec2Bin(int val, int len,char* buffer)
like this
char* Dec2Bin(int val, int len)
and then assigning
buffer=Dec2Bin(val,len)?

i tryed it but i'm too much inexpert about passing arrays and pointers from to a funct... or that's because it's not possible with C language?
 
The way that pops into my head is to declare a new buffer inside of Dec2Bin and its size will be len+1. However, you must remember to delete it yourself to avoid leaking.

The below is untested but the theory is there :)

Matt

Code:
char* pBuf = Dec2Bin(val,len);

... stuff

delete[] pBuf;

....

char* Dec2Bin(int val, int len)
{
 char* retBuf = new char[len+1];
 memset(retBuf,0,len+1);

 for(int i = len-1,j=0;j<len;i--,j++)
 {
   retBuf [j] = ( (val & (1<<(i)) ? '1':'0'));
 }

 return retBuf;
    
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top