Convert c/c++ Object into binary form ( int ,float ,char ..etc to binary )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
After 2hrs effort I have completed this :) | |
HIOS@PRODUCT | |
CODER# @haikent hitesh | |
-> conert any c /c++ object int binary form (int,float,char,struct data.....) | |
<for gudan> | |
*/ | |
#include<iostream> | |
using namespace std; | |
void charBin(char c) | |
{ | |
char ch=1; | |
for(int i=7;i>=0;i--) | |
{ | |
(c&ch<<i)?cout<<"1": cout<<"0"; | |
} | |
} | |
template<class T> | |
void binary(T x) | |
{ | |
unsigned long a=sizeof(x); | |
T *t; | |
t=&x; | |
char *c; | |
c=(char *)t; | |
for(int i=a-1;i>=0;i--){charBin(*(c+i));} cout<<endl; | |
} | |
struct gunjan | |
{ | |
int a; | |
char c; | |
float f; | |
}; | |
int main() | |
{ | |
int i=5; | |
char c='a'; | |
float f=3.36; | |
gunjan g={5,'a',3.36}; | |
binary(i); | |
binary(c); | |
binary(f); | |
binary(g); | |
} |
Comments
Post a Comment