Hi,
I'm facing some problem while returning string. I want to return final string value and print it. Below is the code that i tried,

 int main()
 {
     int Input = 0;

     //Tried using tmpstr also
     //char *tmpStr = NULL;

     scanf_s("%d", &Input);

     //Tried as below also but it was crashing
     //tmpStr = BinaryFormat((void*)Input);

     //Tried as below also but it was crashing and showing only first character
     //tmpStr = (char*)BinaryFormat((void*)Input);
     //printf("\nThe Binary format for the given is : %s",  tmpStr); //It was crashing here

     //Trying to print the number in binary format
     printf("\nThe Binary format for the given is : %s", BinaryFormat((void*)Input) ); //Its crashing here
 }

//
//Coverting the given input to Binary data
//
char* BinaryFormat(void *Input)
{
    char *binFormat = NULL;
    int num = (int)Input;

    //Counting number of bits needed
    int bitCount = BitCount((int)Input); //This will return number of digits needed. This is separate fn

    //Allocating Input based on number of digit(bit value) needed
    //binFormat = (char*) malloc(bitCount*sizeof(char)+1);
    binFormat = (char*) calloc(1,bitCount+1);

    if (Input)
    {
        while(bitCount)
        {
            //Filling binFormat either with 1's or 0's based on number
            binFormat[bitCount-1] = num&1 ? 1 : 0;

            num = num >> 1;
            bitCount--;
        }
    }
    //returning final value as string
    return binFormat;
}

Thanks in advance.

Recommended Answers

All 5 Replies

O.O
I... I'm really not sure what to say here.

#include <stdio.h>
#include <stdlib.h>
//Declare prototypes
unsigned int BitCount(unsigned int);
char *BinaryFormat(void*);

int main()
{
    char *binaryString;
    int Input = 0;

    scanf_s("%d", &Input);
    binaryString=BinaryFormat((void*)&Input); //Use reference to Input instead of Input's value.

    printf("\nThe Binary format for the given is : %s", binaryString); //Its no longer crashing here
    free(binaryString); //Clean up after yourself when using dynamic allocation.
}

//
//Coverting the given input to Binary data
//
char* BinaryFormat(void *Input) //Why void*?
{
    char *binFormat = NULL;
    unsigned int num = *((unsigned int*)Input); //Cast Input as Input pointer, then grab referenced value.

    //Counting number of bits needed
    unsigned int bitCount = BitCount((int)num); //This will return number of digits needed. This is separate fn

    //Allocating Input based on number of digit(bit value) needed
    binFormat = (char*) calloc(1,bitCount+1);

    if (num)
    {
        binFormat[bitCount]=0; //As a precaution.
        while(bitCount)
        {
            //Filling binFormat either with 1's or 0's based on number
            binFormat[bitCount-1] = num&1 ? '1' : '0'; //Use '1' and '0'

            num = num >> 1;
            bitCount--;
        }
    }else{
        binFormat[0]=0; //Send back an empty string
    }
    //returning final value as string
    return binFormat;
}

unsigned int BitCount(unsigned int number){
    unsigned int bits=0; //EDIT: Oops. I forgot to make sure this was initialized to zero.
    while(number){
        number >>= 1;
        bits++;
    }
    return bits;
}

DeanMSands3, Thanks for your prompt reply. Let me try your suggestion.

char* BinaryFormat(void Input) //Why void?

Dean, the reason for the above question is, i'm planning to go for generic input case like get input in any format (int, char, ) and trying to converting to binary format..So i used void. This is very initial phase, so i tried with scanf_s("%d", &Input);

Made a few more edits. I realized I was failing to initialize the bits variable in BitCount. Then I realized this code would also hang on negative numbers. Not sure how you wanted that handled. I went with casting as unsigned which will give you the "Two's Complement" value.

EDIT:
@Perry31:
Ah. I see.

Thanks Dean for your kindly support. It worked as expected..Resolving this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.