0

I pass in a hex number into hex2bin and it prints out the binary number correctly but I don't want it to print out the number I want to return the number so I can use it to find the cardinality of the number. How would I store the number instead of printing it out?

int hex2bin (int n){
    int i,k,mask;
    for(i = sizeof(int) * 8 - 1; i >= 0; i--){
         mask = 1 << i;
         k = n & mask;
         k == 0 ? printf("0"):printf("1");
    }
    return 0;
}
Wesley
  • 89
  • 6
  • The number you pass in 'n' and its binary representation are one in the same as they are all stored as `0`'s and `1`'s in memory. If I understand your question, then you don't need to pass to `hex2bin` to begin with, you simply need to analyze the cardinality instead of passing to `printf` in whatever way you choose. The approach is essentially identical, but you must be careful of your bit order (it is the reverse of printed order) – David C. Rankin Nov 17 '16 at 05:04

4 Answers4

1

Perhaps something like this?

int result = 0;
int i, k...
...
result = result | (((k == 0) ? 0 : 1) << i;
...
return result;

Instead of being clever with an int, you could of course also simply use an array of variables instead.

Eric M.
  • 642
  • 5
  • 16
1

Store the number in a string whose space is provided by a compound literal (Available since C99).

It works like OP's flow: Loop up to sizeof(int) * 8 times, finding the value of 1 bit and print/save it.

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// Maximum buffer size needed
#define UTOA_BASE_2 (sizeof(unsigned)*CHAR_BIT + 1)

char *utoa_base2(char *s, unsigned x) {
  s += UTOA_BASE_2 - 1;
  *s = '\0';
  do {
    *(--s) = "01"[x % 2];
    x /= 2;
  } while (x);
  return s;
}

#define TO_BASE2(x) utoa_base2((char [UTOA_BASE_2]){0} , (x))

void test(unsigned x) {
  printf("base10:%10u base2:%5s ", x, TO_BASE2(x));
  char *s = TO_BASE2(x);
  // do stuff with `s`, it is valid for until the end of this block
  printf("%s\n", s);
}

int main(void) {
  test(0);
  test(25);
  test(UINT_MAX);
}

Sample output

base10:         0 base2:    0 0
base10:        25 base2:11001 11001
base10:4294967295 base2:11111111111111111111111111111111 11111111111111111111111111111111

This is a variation of this base-n answer.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

You can use the strcat function to do that.

Note that the new hex2bin function in this answer assumes that the parameter char *buf has already been allocated and can hold at least 1+sizeof(int)*8 bytes including the null terminator:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// assume: buf is at least length 33
int hex2bin (int n, char *buf)
{
    int i,k,mask;
    for(i = sizeof(int) * 8 - 1; i >= 0; i--){
         mask = 1 << i;
         k = n & mask;
         k == 0 ? strcat(buf, "0") : strcat(buf, "1");
    }
    return 0;
}

int main()
{
    int n = 66555;
    char buffer[1+sizeof(int)*8] = { 0 } ;
    hex2bin(n, buffer);
    printf("%s\n", buffer);
    return 0;
}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
0

I hope you will find this helpful :)

bool convertDecimalBNR(INT32 nDecimalValue, UINT32 * punFieldValue, INT32 nBitCount, DecimalBNRType * pDecimalSpecification)
{
  bool   bBNRConverted = false;
  INT32  nBitIndex = nBitCount - 1;
  INT32  nBitValue = anTwoExponents[nBitIndex];

  *punFieldValue = 0;

  if ((nDecimalValue >= pDecimalSpecification->nMinValue) && (nDecimalValue <= pDecimalSpecification->nMaxValue))
  {
        // if the value is negative, then add (-1 * (2 ^ (nBitCount - 1))) on itself and go on just like a positive value calculation.
    if (nDecimalValue < 0)
    {
      nDecimalValue += nBitValue;
      nBitIndex--;
      nBitValue /= 2;
      *punFieldValue |= BIT_0_ONLY_ONE;
    }

    while (nBitIndex >= 0)
    {
      *punFieldValue = (*punFieldValue << 1);

      if (nDecimalValue >= nBitValue)
      {
        nDecimalValue -= nBitValue;
        *punFieldValue |= BIT_0_ONLY_ONE;
      }

      nBitIndex--;
      nBitValue /= 2;
    }

    if (nDecimalValue <= nBitValue)
    {
      bBNRConverted = true;
    }
  }

  return (bBNRConverted);
}
Sanberk
  • 51
  • 1
  • 2
  • How would I change this for base64? – Wesley Nov 17 '16 at 13:44
  • @ShaolinGOD What do you mean by base64? If you are asking for 64 bits, then you should use "typedef long INT64;" instead of using "typedef int INT32;" i think. – Sanberk Nov 17 '16 at 14:59