ゴール
私の目標は、大きなバイナリ文字列 (1 と 0 のみを含む文字列)からファイルをすばやく作成することです。
単刀直入に
目標を達成できる機能が必要です。よくわからない場合は、読み進めてください。
例
Test.exe is running...
.
Inputted binary string:
1111111110101010
Writing to: c:\users\admin\desktop\Test.txt
Done!
File(Test.txt) In Byte(s):
0xFF, 0xAA
.
Test.exe executed successfully!
説明
- 最初に、Test.exe はユーザーにバイナリ文字列の入力を要求しました。
- 次に、入力されたバイナリ文字列を 16 進数に変換しました。
- 最後に、変換された値を Test.txt というファイルに書き込みました。
私はもう試した
私の目標を達成するための失敗した試みとして、私はこの単純な (そしておそらく恐ろしい) 関数を作成しました (ねえ、少なくとも私は試しました):
void BinaryStrToFile( __in const char* Destination,
__in std::string &BinaryStr )
{
std::ofstream OutputFile( Destination, std::ofstream::binary );
for( ::UINT Index1 = 0, Dec = 0;
// 8-Bit binary.
Index1 != BinaryStr.length( )/8;
// Get the next set of binary value.
// Write the decimal value as unsigned char to file.
// Reset decimal value to 0.
++ Index1, OutputFile << ( ::BYTE )Dec, Dec = 0 )
{
// Convert the 8-bit binary to hexadecimal using the
// positional notation method - this is how its done:
// http://www.wikihow.com/Convert-from-Binary-to-Decimal
for( ::UINT Index2 = 7, Inc = 1; Index2 + 1 != 0; -- Index2, Inc += Inc )
if( BinaryStr.substr( Index1 * 8, 8 )[ Index2 ] == '1' ) Dec += Inc;
}
OutputFile.close( );
};
使用例
#include "Global.h"
void BinaryStrToFile( __in const char* Destination,
__in std::string &BinaryStr );
int main( void )
{
std::string Bin = "";
// Create a binary string that is a size of 9.53674 mb
// Note: The creation of this string will take awhile.
// However, I only start to calculate the speed of writing
// and converting after it is done generating the string.
// This string is just created for an example.
std::cout << "Generating...\n";
while( Bin.length( ) != 80000000 )
Bin += "10101010";
std::cout << "Writing...\n";
BinaryStrToFile( "c:\\users\\admin\\desktop\\Test.txt", Bin );
std::cout << "Done!\n";
#ifdef IS_DEBUGGING
std::cout << "Paused...\n";
::getchar( );
#endif
return( 0 );
};
問題
繰り返しますが、それは私の目標を達成するための私の失敗した試みでした. 問題は速度です。遅すぎる。7分以上かかりました。大きなバイナリ文字列からファイルをすばやく作成する方法はありますか?
前もって感謝します、
C学習者