Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JKBGL/BinaryFileReader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

BinaryFileReader

Here we have a basic c++ binary reader I wrote for an unrelated project that required it.
I decided to post in on github in case some people needed it. ♥

Features:

  • Reading:
    • int (Any bit size, with endian adjustability)
    • char Array
    • string (very unstable)
  • Converting:
    • bytes to int (any size with little_endian support)
    • bytes to char array
    • bytes to std::string
  • Misc:
    • Displaying byte array to console in hex format

Contributions:

If you find this library useful and have extensive knowledge in c++
and want to make it better, please do create a pull request
with proposed features, fixes or overall better implementations of
the methods this library uses.

Docs:


Example program featuring everything in the library:

#include "BinaryReader.h"
#define byte unsigned char
void main() {
 // Opening the file we will read from
 BinaryFileReader a("test.bin");
 // This will read a string and store it in the variable "name"
 // and will also store its size in the "name_size" variable
 size_t name_size = 0;
 byte *name = a.ReadBytesUntilBreak(name_size);
 // convert bytes to a usable char array
 char* str_name = BinaryFileReader::ConvertBytesToCharArray(name, name_size);
 // Reading the next 32 bytes from the file
 byte *t_hash = a.ReadBytes(32);
 // This will display the hex of the "t_hash" variable (useful for comparison of boundaries
 // or checking if the reader is reading correctly)
 BinaryFileReader::DisplayBytesHex(t_hash, 32);
 // Read a 3 byte int value
 byte* key_id_bytes = a.ReadBytes(3);
 // Convert a 3 byte int value to usable int variable
 int key_id = BinaryFileReader::ConvertBytesToAnyInt(key_id_bytes, 3);
 // display key_id_bytes
 BinaryFileReader::DisplayBytesHex(key_id_bytes, 3);
 // Read a 16bit int
 int16_t key_flags = a.ReadInt16();
 // Read a 32bit int
 int name_flags = a.ReadInt32();
 
 // Closing the binary file
 a.close();
 
 ...
 
 return;
}

Reading

Opening a file:

  • Directly in the constructor:
    BinaryFileReader a("test.bin");
  • With the open() method:
    BinaryFileReader a;
    a.open("test.bin");

Closing an opened file:

a.close();

Note: the object will automatically close the file if out of scope


Read a 16bit int:

a.ReadInt16();

returns a usable int16_t


Read a 32bit int:

a.ReadInt32();

returns a usable int


Read a single byte:

a.ReadByte();

returns an unsigned char


Read bytes:

a.ReadBytes(40);

returns unsigned char*
Note: limited to 500 bytes per call


Read string with unknown size (char*):

size_t size_variable;
a.ReadBytesUntilBreak(size_variable);

returns unsigned char*
writes the size of the string to the size variable


Read string with unknown size (std::string):

a.ReadString();

returns std::string
Note: This function may show unwanted/unstable behaviour, use ReadBytesUntilBreak()




Converting

Convert byte array to an integer:

int result = BinaryFileReader::ConvertBytesToAnyInt(byte_arr, 4);

returns an int


Convert byte array to an integer (endian toggle):

int result = BinaryFileReader::ConvertBytesToAnyInt2(byte_arr, 4, true);

returns an int


Convert byte array to char* array:

char *result = BinaryFileReader::ConvertBytesToCharArray(byte_arr, byte_arr_size);

returns char*




Misc

View byte array in console:

BinaryFileReader::DisplayBytesHex(byte_arr, byte_arr_size);

returns char*

About

A basic c++ binary reader with c# like syntax.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /