npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

bytereader

v0.3.0

Published

An npm package for reading binary files written in TypeScript

Downloads

12

Readme

ByteReader

The ByteReader class is a utility for reading binary data from an ArrayBuffer object. It provides methods for reading various types of data such as integers, floats, strings, and extracting subarrays.

Usage

To use the ByteReader class, you can import it as a default export:

import ByteReader from 'ByteReader';

Creating a ByteReader

To create an instance of ByteReader, you need to provide an ArrayBuffer object containing the binary data you want to read. Additionally, you can specify whether the data should be interpreted in little endian or big endian format (little endian by default).

const bytes = new ArrayBuffer(/* ... */);
const reader = new ByteReader(bytes, true);

Seeking

The seek method allows you to set the current position within the binary data. It takes an offset parameter that specifies the new position.

reader.seek(10); // Set the position to the 10th byte

The seekRel method is similar to seek, but it moves the position relative to the current position.

reader.seekRel(5); // Move the position 5 bytes forward

The seekEnd method sets the position relative to the end of the data. It expects a negative whence argument indicating the offset from the end.

reader.seekEnd(-3); // Set the position to 3 bytes before the end

Reading Data

The ByteReader class provides several methods for reading different types of data:

  • readInt8: Reads a signed 8-bit integer and advances the position by 1 byte.
  • readUInt8: Reads an unsigned 8-bit integer and advances the position by 1 byte.
  • readInt16: Reads a signed 16-bit integer and advances the position by 2 bytes.
  • readUInt16: Reads an unsigned 16-bit integer and advances the position by 2 bytes.
  • readInt32: Reads a signed 32-bit integer and advances the position by 4 bytes.
  • readUInt32: Reads an unsigned 32-bit integer and advances the position by 4 bytes.
  • readFloat: Reads a 32-bit floating-point number and advances the position by 4 bytes.
  • readString: Reads a string of characters until a null byte (0) is encountered or until a specified length is reached. Advances the position accordingly.
const int8 = reader.readInt8(); // Read a signed 8-bit integer
const uint16 = reader.readUInt16(); // Read an unsigned 16-bit integer
const float = reader.readFloat(); // Read a floating-point number
const str = reader.readString(10); // Read a string of up to 10 characters

Current Position

To retrieve the current position within the binary data, you can use the tell method, which returns the offset in bytes.

const position = reader.tell(); // Get the current position

The tellf method returns a formatted string representing the current position in hexadecimal format.

const positionStr = reader.tellf(); // Get the current position as a hexadecimal string

Extracting Subarrays

The subArray method allows you to extract a subarray from the original ArrayBuffer based on start and end offsets. The resulting subarray is returned as a new ArrayBuffer object.

const sub = reader.subArray(10, 20); // Extract a subarray from position 10 to 20 (exclusive)

Converting to String

The toString method allows you to convert a range of bytes within the ByteReader to a string. It takes a start offset and an end offset (exclusive) as parameters and returns the corresponding string representation.

const str = reader.toString(10, 20); // Convert bytes from position 10 to 20 (exclusive) to a string

Please note that this method interprets the bytes as signed 8-bit integers and converts them to their corresponding ASCII characters.

Example Usage

Here's an example that demonstrates the usage of the ByteReader class:

const bytes = new ArrayBuffer(/* ... */);
const reader = new ByteReader(bytes);

// Read some data
const int16 = reader.readInt16();
const float = reader.readFloat();
const str = reader.readString(10);

// Get current position
const position = reader.tell();

// Extract a subarray
const sub = reader.subArray(10, 20);

// Convert a range of bytes to string
const strRange = reader.toString(5, 15);

In this example, we create a ByteReader instance, read different types of data, get the current position, extract a subarray, and convert a range of bytes to a string.

Remember to adjust the method calls and parameters according to your specific use case and binary data format.

Conclusion

The ByteReader class provides a convenient way to read binary data from an ArrayBuffer. It offers methods for seeking, reading different types of data, retrieving the current position, extracting subarrays, and converting byte ranges to strings. This utility can be helpful when working with binary file formats, network protocols, or any situation that requires parsing binary data.

Copyright

Copyright 2023 DashGL Project MIT License