0

I have an array of bytes as integers in base 256. So it looks like [0, 120, 255, 30, 21]. Each number represents a byte, so like 0 = 0000000, 1 = 0000001, 2 = 00000010, etc...

How can I write this array to a file? I am really lost I don't know where to begin with this.

1
  • How do you have "an array of bytes in base 256"? Do you have strings that shall be converted? Or do you really have an array of bytes (i. e. unsigned chars) - in which case, there's no base-256 whatsoever? Commented Aug 21, 2013 at 5:25

3 Answers 3

2

Here is a simple example:

#include <fstream.h>

char buffer[100] = {0, 120, 255, 30, 21, ... };
ofstream myFile ("data.bin", ios::out | ios::binary);
myFile.write (buffer, 100);
Sign up to request clarification or add additional context in comments.

Comments

0

You can open the file in binary mode in C++ with fstream like this http://www.cplusplus.com/forum/general/11272/.

Or open a file in C compatible fashion,

fopen("test.bin", "bw+");

Comments

0

You want something like:

ofstream my_bin_file;
my_bin_file.open("filename.bin", ios::out | ios::binary);
// some loop to get your int byte's
    my_bin_file << byte;
my_bin_file.close();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.