我想讀的ANSI格式的文件,並轉換這binary.I'm聲明兩個動態內存分配是這樣的:char* binary_reverse = new char;
和char * binary = new char;
新的聲明包含垃圾值和堆腐敗而使用delete
雖然調試我看到這(二進制)包含太多的垃圾值。爲什麼這樣?
我正在刪除這些:delete binary_reverse;刪除二進制; 然而,在刪除其給我的錯誤:
'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.
這裏是我正在做代碼:
#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;
}
}
我想在「二進制」的準確的二進制值。不是垃圾的二進制值?如何消除垃圾值?如何避免堆腐敗?
'char * binary_reverse = new char; ''和'char * binary = new char;' - 所以你分配1個字節用於存儲 – RbMm
關閉主題:保存一些代碼:'else if(ascii%2 == 1)'可以只是'else'。當處理一個單一的二進制位時,它的值將是1或0.如果它不是一個,它必須是另一個。 – user4581301