I'm trying to read an ANSI formatted file and converting this to binary.I'm declaring two dynamic memory allocation like this: char* binary_reverse = new char;
and char * binary = new char;
While debugging I see this(binary) contains too many garbage values. Why is it so?
I'm deleting these like: delete binary_reverse; delete binary; However, during delete its giving me error:
'ASCIItoBinary.exe': Loaded 'D:\TryingBest\Reactice\ASCIItoBinary\Debug\ASCIItoBinary.exe', Symbols loaded. 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file 'ASCIItoBinary.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded. HEAP[ASCIItoBinary.exe]: Heap block at 00241ED0 modified at 00241EFD past requested size of 25 Windows has triggered a breakpoint in ASCIItoBinary.exe.
Here is how I'm doing code:
#include <cstring>
void AtoB(char * input)
{
unsigned int ascii; //used to store ASCII number of a character
unsigned int length = strlen(input);
//cout << " ";
for (int x = 0; x < length; x++) //repeat until the input is read
{
ascii = input[x];
char* binary_reverse = new char; //dynamic memory allocation
char * binary = new char;
//char binary[8];
int y = 0;
while (ascii != 1)
{
if (ascii % 2 == 0) //if ascii is divisible by 2
{
binary_reverse[y] = '0'; //then put a zero
}
else if (ascii % 2 == 1) //if it isnt divisible by 2
{
binary_reverse[y] = '1'; //then put a 1
}
ascii /= 2; //find the quotient of ascii / 2
y++; //add 1 to y for next loop
}
if (ascii == 1) //when ascii is 1, we have to add 1 to the beginning
{
binary_reverse[y] = '1';
y++;
}
if (y < 8) //add zeros to the end of string if not 8 characters (1 byte)
{
for (; y < 8; y++) //add until binary_reverse[7] (8th element)
{
binary_reverse[y] = '0';
}
}
for (int z = 0; z < 8; z++) //our array is reversed. put the numbers in the rigth order (last comes first)
{
binary[z] = binary_reverse[7 - z];
}
//printf("the Binary is %s",binary);
//cout << binary; //display the 8 digit binary number
delete binary_reverse; //free the memory created by dynamic mem. allocation
delete binary;
}
}
I want the exact binary values in "binary". NOT the binary values along with garbage?How to eliminate the garbage values? How to avoid the heap corruption?
char* binary_reverse = new char;
andchar * binary = new char;
- so you allocate 1 byte for storage – RbMmelse if (ascii % 2 == 1)
can just beelse
. When dealing with a single binary bit it's value will either be 1 or 0. If it's not one, it must be the other. – user4581301