I have a txt file with numbers like 541399.531 261032.266 16.660 (first line) 541400.288 261032.284 16.642 (2nd line)........hundred of points. i want to convert this file i
There is binmake an open source C++ tool allowing to convert text data to binary data. It currently manages several number representations and raw text (hexa, octal, floats..).
I think it is interesting to mention it here as the title deals with text to binary file in C++ what binmake can do.
It can be used as a standalone binary but also included in your C++ code.
With stdin
/stdout
:
$ echo '32 decimal 32 %x61 61' | ./binmake | hexdump -C
00000000 32 20 61 3d |2 a=|
00000004
With files:
$ ./binmake exemple.txt exemple.bin
(see below for a sample view)
There's some examples of use:
#include
#include "BinStream.h"
using namespace std;
using namespace BS;
int main()
{
BinStream bin;
bin << "'hello world!'"
<< "00112233"
<< "big-endian"
<< "00112233";
ofstream f("test.bin");
bin >> f;
return 0;
}
Or
#include
#include "BinStream.h"
using namespace std;
int main()
{
BS::BinStream bin;
ifstream inf("example.txt");
ofstream ouf("example.bin");
bin << inf >> ouf;
return 0;
}
Or
#include
#include "BinStream.h"
using namespace std;
using namespace BS;
int main()
{
BinStream bin;
cin >> bin;
cout << bin;
return 0;
}
File exemple.txt
:
# an exemple of file description of binary data to generate
# set endianess to big-endian
big-endian
# default number is hexadecimal
00112233
# man can explicit a number type: %b means binary number
%b0100110111100000
# change endianess to little-endian
little-endian
# if no explicit, use default
44556677
# bytes are not concerned by endianess
88 99 aa bb
# change default to decimal
decimal
# following number is now decimal
0123
# strings are delimited by " or '
"this is some raw string"
# explicit hexa number starts with %x
%xff
The generated binary output:
$ ./binmake exemple.txt | hexdump -C
00000000 00 11 22 33 4d e0 77 66 55 44 88 99 aa bb 7b 74 |.."3M.wfUD....{t|
00000010 68 69 73 20 69 73 20 73 6f 6d 65 20 72 61 77 20 |his is some raw |
00000020 73 74 72 69 6e 67 ff |string.|
00000027