Convert Types to String

serkan sendur 0 Tallied Votes 232 Views Share

Converts various types to string

#include <stdio.h>

void dbiIntToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "%d", *(int*)SrcData);
   return;
}
void dbiCharToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "%c", *(char*)SrcData);
   return;
}
void dbiShortToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "%hd", *(short*)SrcData);
   return;
}
void dbiHexCharToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "0x%hX", *(char*)SrcData);
   return;
}
void dbiHexShortToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "0x%hX", *(short*)SrcData);
   return;
}
void dbiHexIntToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "0x%X", *(int*)SrcData);
   return;
}
void dbiByteToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "%s", (char*)SrcData);
   return;
}
void dbiChrTToStr(void* SrcData, char* DestStr)
{
   sprintf(DestStr, "%s", (char*)SrcData);
   return;
}

int main()
{
  char  pDest[1000];

  
  int den = 12;
  int * pDenI = &den;
  dbiIntToStr(pDenI,pDest);
  printf("%s \n", pDest);

  
  char * pDen = "a1";
  dbiCharToStr(pDen,pDest);
  printf("%s \n", pDest);

  short int denS = 13;
  short int * pDenS = &denS;
  dbiShortToStr(pDenS,pDest);
  printf("%s \n", pDest);
 
  char denHC = 'z';
  char * pDenHC = &denHC;
  dbiHexCharToStr(pDenHC,pDest);
  printf("%s \n",pDest);
  
  
  short int denHS = 253;
  short int * pDenHS = &denHS;
  dbiHexShortToStr(pDenHS,pDest);
  printf("%s \n", pDest);
 
  int denHI = 252;
  int * pDenHI = &denHI;
  dbiHexIntToStr(pDenHI,pDest);
  printf("%s \n",pDest);

  char * pDenB = "10101010";
  dbiByteToStr(pDenB,pDest);
  printf("%s \n",pDest);
  
  char * pDenCT = "garg245arg";
  dbiChrTToStr(pDenCT,pDest);
  printf("%s \n",pDest);
  
  return 0;
}
Dave Sinkula 2,398 long time no c Team Colleague

Why the silliness of having the second parameter be the wrong pointer type, in half the cases, and then casting (redundantly half the time)?

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.