Hello guys this is my code.
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
void charToBinary( char *value ) {
unsigned int i;
unsigned int alloc = (strlen(value)+1);
int* cVec = (int*)malloc(alloc);
for( i = 0; i < strlen(value); i++ ) {
//works in windows but not on linux
//cVec[ i ] = int(value[ i ]);
cVec[ i ] = (int)(value[ i ]);
}//for
printf("\n");
printf("The typed string into Ascii mode: ");
for( i = 0; i < strlen(value); i++ )
printf("%d ",cVec[ i ]);printf(" ");printf("\n");
printf("-----------------------------------------\n");
int index,
bit;
for(index = 0 ; index < strlen(value); index++ ) {
for(bit = 8; bit >0; bit-- ) {
//here is my bug
// it segmentation fault using eclipse CDT.
// By using Borland C++ there's no problem.
printf("%d",((cVec[ index ]&(1 << bit))? 1 : 0));
}
printf(" ");
}
free( cVec );
};
//==========================================================
int main( ) {
//string strValue;
char str[] = { 0 };
//copyStr = (char*)malloc(strValue.length() + 1);
printf("Enter a string for convertion : ");
//system("pause");
scanf("%s",&str );
//strcpy(copyStr, strValue.c_str());
charToBinary( str );
printf("\n");
//free( copyStr );
system("pause");
}
Can anyone help me please???