// it's caller's responsibility to delete the memory
char * conv_int_to_binary(int data)
{
int size = sizeof(data)*8;
char *binString = new char(size);
for ( int i = 0; i < size; i++)
{
binString[i] = (data >> i)&1;
}
return binString;
}
reverse string at the end
or --> start from binString[size], with size --
From: http://www.glassdoor.com/Interview/Write-a-function-to-obtain-a-string-with-the-binary-representation-of-an-integer-QTN_132949.htm
No comments:
Post a Comment