I'm making a program that writes a binary file, the content of this binary file are in an unsigned char pointer. The next code is the way that I using to generate the binary
while (f > 0) {
oct = (unsigned char) *datosOct->informacion++;
for (int i = 2; i >= 0; --i)
{
char actual = ((oct & (1 << i)) ? '1' : '0');
temporal = appendCharToCharArray(temporal, actual);
}
f--;
}
datosBin->informacion = (unsigned char*)temporal;
I had used fopen in mode wb, but it literals write a file with 1s and 0s. this is the function that I use to write (I using the default compiler of Visual Studio). I use the next code to write the file
file = fopen(nombreArchivo, "wb");
fwrite(datosBin->informacion, sizeof(char), archivoEnOctal->tamanio, file);
My starting point is a char array that contains '1's and '0's, each byte of '1's and '0's represents an ASCII value. For example, '1010000' is the ASCII 80 representing the letter 'P', if I transforms the binary char array ('1010000') to an array of chars ('P') when it finds values whose ASCII is 00 takes them as the end of the text and does not write them to the final file. One of the possible outputs of my code is an image, so I need to write these values 00. I tried this to write the chars directly, but this doesn't work for the images for the reason I mentioned before
while (f > 0) {
oct = (unsigned char) *datosOct->informacion++;
for (int i = 2; i >= 0; --i)
{
if (size == 8) {
size = 0;
char nuevaLetra = strtol(temporal, 0, 2);
if (nuevaLetra == 00) {
nuevaLetra = '\0';
//nuevaLetra = (char) 0;
}
response = appendCharToCharArray(response, nuevaLetra);
temporal = (char*)"";
}
size++;
char actual = ((oct & (1 << i)) ? 1 : 0);
temporal = appendCharToCharArray(temporal, actual);
}
f--;
}
char actual = ((oct & (1 << i)) ? '1' : '0');assigns the character'0'or'1'not the value0or1.char actual = ((oct & (1 << i)) ? 1 : 0);is not the same aschar actual = ((oct & (1 << i)) ? '1' : '0');- your last comment is untrue.