Click here to Skip to main content
15,881,709 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: [edited]Re: pointers to functions Pin
Calin Negru16-Jul-22 6:57
Calin Negru16-Jul-22 6:57 
GeneralRe: [edited]Re: pointers to functions Pin
Richard MacCutchan16-Jul-22 20:58
mveRichard MacCutchan16-Jul-22 20:58 
AnswerRe: pointers to functions Pin
Greg Utas16-Jul-22 7:36
professionalGreg Utas16-Jul-22 7:36 
QuestionConvert decimal to binary in C Pin
sahil Ranka14-Jul-22 1:14
sahil Ranka14-Jul-22 1:14 
AnswerRe: Convert decimal to binary in C Pin
Mircea Neacsu14-Jul-22 1:25
Mircea Neacsu14-Jul-22 1:25 
AnswerRe: Convert decimal to binary in C Pin
Richard MacCutchan14-Jul-22 1:29
mveRichard MacCutchan14-Jul-22 1:29 
GeneralRe: Convert decimal to binary in C Pin
sahil Ranka14-Jul-22 2:42
sahil Ranka14-Jul-22 2:42 
AnswerRe: Convert decimal to binary in C Pin
CPallini14-Jul-22 2:36
mveCPallini14-Jul-22 2:36 
As noted in other answers, your code handles at most four bits.
Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output.
Try
C
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>

bool uint2binstr(unsigned value, char output[], size_t output_size)
{
  unsigned v = value;
  unsigned bits = 0;
  while ( v )
  {
    v >>= 1;
    ++bits;
  }
  if ( ! bits ) ++bits;

  if ( output_size > bits )
  {
    output[bits] = '\0';
    while (bits)
    {
      --bits;
      output[bits] = (value & 1) + '0';
      value >>= 1;
    }
    return true;
  }
  return false;
}

enum { N = 33 }; // assuming 'unsigned' is 32 bits

int main()
{
  char out[N];

  unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };

  for (size_t n=0; n<sizeof(a)/sizeof(a[0]); ++n)
  {
    if ( uint2binstr(a[n], out, N) )
    {
      printf("success: '%u' decimal is '%s' binary\n", a[n], out);
    }
    else
    {
      printf("failed to represent '%u' in binary\n", a[n]);
    }
  }
  return 0;
}

"In testa che avete, Signor di Ceprano?"
-- Rigoletto

GeneralRe: Convert decimal to binary in C Pin
sahil Ranka14-Jul-22 2:42
sahil Ranka14-Jul-22 2:42 
GeneralRe: Convert decimal to binary in C Pin
CPallini14-Jul-22 3:14
mveCPallini14-Jul-22 3:14 
AnswerRe: Convert decimal to binary in C Pin
Richard MacCutchan14-Jul-22 21:15
mveRichard MacCutchan14-Jul-22 21:15 
QuestionPrey/predator c++ project Pin
sahil Ranka12-Jul-22 0:26
sahil Ranka12-Jul-22 0:26 
AnswerRe: Prey/predator c++ project Pin
Richard MacCutchan12-Jul-22 0:29
mveRichard MacCutchan12-Jul-22 0:29 
AnswerRe: Prey/predator c++ project Pin
Greg Utas12-Jul-22 1:20
professionalGreg Utas12-Jul-22 1:20 
QuestionHow to share a pointer between an EXE and a DLL Pin
Mircea Neacsu11-Jul-22 13:50
Mircea Neacsu11-Jul-22 13:50 
QuestionRe: How to share a pointer between an EXE and a DLL Pin
CPallini11-Jul-22 20:12
mveCPallini11-Jul-22 20:12 
AnswerRe: How to share a pointer between an EXE and a DLL Pin
Mircea Neacsu12-Jul-22 4:01
Mircea Neacsu12-Jul-22 4:01 
GeneralRe: How to share a pointer between an EXE and a DLL Pin
Victor Nijegorodov12-Jul-22 6:22
Victor Nijegorodov12-Jul-22 6:22 
GeneralRe: How to share a pointer between an EXE and a DLL Pin
Mircea Neacsu12-Jul-22 7:52
Mircea Neacsu12-Jul-22 7:52 
GeneralRe: How to share a pointer between an EXE and a DLL Pin
Victor Nijegorodov12-Jul-22 8:04
Victor Nijegorodov12-Jul-22 8:04 
QuestionRe: How to share a pointer between an EXE and a DLL Pin
JudyL_MD12-Jul-22 11:31
JudyL_MD12-Jul-22 11:31 
GeneralRe: How to share a pointer between an EXE and a DLL Pin
Mircea Neacsu12-Jul-22 12:17
Mircea Neacsu12-Jul-22 12:17 
GeneralRe: How to share a pointer between an EXE and a DLL Pin
JudyL_MD12-Jul-22 13:29
JudyL_MD12-Jul-22 13:29 
AnswerRe: How to share a pointer between an EXE and a DLL Pin
Graham Breach11-Jul-22 20:30
Graham Breach11-Jul-22 20:30 
AnswerRe: How to share a pointer between an EXE and a DLL Pin
Gerry Schmitz12-Jul-22 6:57
mveGerry Schmitz12-Jul-22 6:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.