0

Here I have created a string and I am storing the binary value of a number in the string.. I want to store the value of the variable num to the string.

i contains the length of the binary number for the given decimal number..suppose the given number is A=6, i contains 3 and i need a string 'result' having '110' which is the binary value of 6.

char* result = (char *)malloc((i)* sizeof(char));
i--;
while(A>=1)
    {
        num=A%2; 
        result[i]=num;  // here I need to store the value of num in the string
        A=A/2;
        i--;
     }
IlGala
  • 3,331
  • 4
  • 35
  • 49
Code_Run
  • 1
  • 1
  • 4
  • 2
    Generic warning: Do not cast the result of `malloc()`. – fuz Nov 12 '15 at 10:26
  • What are these brackets? What contains `i`? If you want to *convert* a number into a string you have to do it explicitly, i.e. using `sprintf` or similar. – hexasoft Nov 12 '15 at 10:29
  • i contains the length of the binary number for the given decimal number..suppose the given number is A=6, i contains 3 and i need a string 'result' having '110' which is the binary value of 6. – Code_Run Nov 12 '15 at 10:31
  • `result` has `i` elements. index is `0`..`i-1`. also C-String need null terminated. – BLUEPIXY Nov 12 '15 at 10:32

5 Answers5

0

It appears from the code you've posted is that what you are trying to do is to print a number in binary in a fixed precision. Assuming that's what you want to do, something like

unsigned int mask = 1 << (i - 1);     
unsigned int pos = 0;
while (mask != 0) {
    result[pos] = (A & mask) == 0 ? '0' : '1';
    ++pos;
    mask >>= 1;
}
result[pos] = 0; //If you need a null terminated string

edge cases left as an exercise for the reader.

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
0

I'm not sure specifically what you are asking for. Do you mean the binary representation (i.e. 00001000) of a number written into a string or converting the variable to a string (i.e. 8)? I'll assume you mean the first.

The easiest way to do this is to repeatedly test the least significant bit and shift the value to the right (>>). We can do this in for loop. However you will need to know how many bits you need to read. We can do this with sizeof.

int i = 15;
for (int b = 0; b < sizeof(i); ++b) {
    uint8_t bit_value = (i & 0x1);
    i >>= 1;
}

So how do we turn this iteration into a string? We need to construct the string in reverse. We know how many bits are needed, so we can create a string buffer accordingly with an extra byte for NULL termination.

char *buffer = calloc(sizeof(i) + 1, sizeof(char));

What this does is allocates memory that is sizeof(i) + 1 elements long where each element is sizeof(char), and then zero's each element. Now lets put the bits into the string.

for (int b = 0; b < sizeof(i); ++b) {
    uint8_t bit_value = (i & 0x1);

    size_t offset = sizeof(i) - 1 - b;
    buffer[offset] = '0' + bit_value;

    i >>= 1;
}

So what's happening here? In each pass we're calculating the offset in the buffer that we should be writing a value to, and then we're adding the ASCII value of 0 to bit_value as we write it into the buffer.

This code is untested and may have some issues, but that is left as an exercise to the reader. If you have any questions, let me know!

Tom Hancocks
  • 440
  • 2
  • 10
0

here is the whole code. It is supposed to work fine.

int i=0;
int A;//supposed entered by user

//calculating the value of i
while(A!=0)
{
    A=A/2;
    i++;
}
char* result=(char *)malloc(sizeof(char)*i);
i--;
while(A!=0)
{
    result[i]='0'+(A%2);
    A=A/2;
    i--;
}
A. Harkous
  • 252
  • 1
  • 3
  • 14
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

char *numToBinStr(int num){
    static char bin[sizeof(int) * CHAR_BIT + 1];
    char *p = &bin[sizeof(int) * CHAR_BIT];//p point to end
    unsigned A = (unsigned)num;

    do {
        *--p = '0' + (A & 1);
        A >>= 1;
    }while(A > 0);//do-while for case value of A is 0 
    return p;
}

int main(void){
    printf("%s\n", numToBinStr(6));
    //To duplicate, if necessary
    //char *bin = strdup(numToBinStr(6));
    char *result = numToBinStr(6);
    char *bin = malloc(strlen(result) + 1);
    strcpy(bin, result);
    printf("%s\n", bin);
    free(bin);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
-1

You could use these functions in <stdlib.h>:

itoa(); or sprintf()

The second link has some examples as well.

Pang
  • 9,564
  • 146
  • 81
  • 122
Vinay Madapura
  • 327
  • 5
  • 17