Markus Luxner
Markus Luxner

Reputation: 3

Remove leading zeros in c

I want to Convert decimal into Binary but the output should be without leading zeros, so how do I remove the zeros? (The code works, it's written in C)

int* dec2bin(int, int[]);

main(){
  int var=0;
  int n[16];
  printf("Number(>=0, <65535): ");
  scanf("%d", &var);
  dec2bin(var, n);
  printf(" %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d\n", n[0], n[1],n[2],n[3],n[4],n[5],n[6],n[7],n[8],n[9],n[10],n[11],n[12],n[13],n[14],n[15]);
}

int* dec2bin(int N, int n[]){
  int count = 15;
  for(count=15; count >=0; count--){
    n[count] = N%2;
    N=N/2;
  }
}

Upvotes: 0

Views: 6310

Answers (2)

Clifford
Clifford

Reputation: 93466

// Skip leading zeros
int d = 0 ;
for( d = 0; n[d] == 0 && d < 15; d++ )
{
    // nothing
}   

// Print significant digits
for( ; d < 16; d++ )
{
    printf( "%d ", n[d] ) ;
}    

Note that dec2bin generates an array of binary digits in an int. That is clearly not a conversion from decimal to binary, because the int already is binary - the scanf() call already did the conversion to binary (int) with the %d specifier. The function is over-complex given that it already is binary. What you are actually doing is simply expanding single bits to an array of integer values 0 and 1.

Consider:

int* int2bin( int N, int n[] )
{
  for( int d = 15; d >= 0; d-- )
  {
    n[d] = (N & (0x0001 << d)) == 0 ? 0 : 1 ;
  }

  return n ;
}

However it perhaps makes more sense to generate a string as ASCII digits rather than an array of integers.

char* int2bin( int val, char* str )
{
    // Skip leading zeros
    int d = 0 ;
    for( d = 0; (val & (1<<d)) == 0 && d < 15; d++ )
    {
        // nothing
    }   

    // Significant digits
    for( int s = 0 ; d < 16; s++; d++ )
    {
        str[s] = (val & (1<<d)) == 0 ? '0' : '1' ;
    }    

    str[s] = 0 ;

    return str ;
}

Then the output is simply:

  char n[17] ;
  printf("%s", int2bin(var, n) ) ;

Upvotes: 1

vitruvius
vitruvius

Reputation: 21139

You have a few issues here.

  • Type specifier of main() is missing. A decent compiler should give you a warning message like this: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
    • I'm not sure which standard you are using, in C89 the default return type is assumed to be int.
    • If you change your main() to int main(), then you should return an integer value or EXIT_SUCCESS if you include stdlib.h.
    • return 0; indicates that program executed successfully.
  • You are not returning anything from your function.
    • Well, you do not need to, since you are passing an array, the array will be modified. You are actually passing a pointer.

What you can do is loop over your array, look for the first "1" and then start printing. You can use a flag for that.

Note: If the user enters "0", just print "0", no need to call your function.

Here is an example:

#include <stdio.h>

typedef enum { false, true } bool;
void dec2bin(int, int[]);

int main()
{
  int var=0;
  const int size = 16;
  int n[size];
  bool startPrinting = false;

  printf("Number(>=0, <65535): ");
  scanf("%d", &var);

  if (var == 0)
  {
    printf("0\n");
  }
  else
  {
    dec2bin(var, n);
    for (int cnt = 0; cnt < size; cnt++)
    {
      if (n[cnt] == 1) // Check for the first "1" in n
      {
        startPrinting = true;
      }

      if (startPrinting)
      {
        printf("%d", n[cnt]);
      }
    }
    printf("\n");
  }
  return 0;
}

void dec2bin(int N, int n[]){
  int count = 15;
  for(count=15; count >=0; count--){
    n[count] = N%2;
    N=N/2;
  }
}

Here is some sample output:

> ./out 
Number(>=0, <65535): 32
100000
> ./out 
Number(>=0, <65535): 16
10000
> ./out 
Number(>=0, <65535): 15
1111
> ./out
Number(>=0, <65535): 1
1
> ./out
Number(>=0, <65535): 0
0

Upvotes: 0

Related Questions