Skip to main content

The QCAT Standard: Understanding Byte Encodings for Length Representation



In programming, it’s common to deal with lengths or sizes of data. Depending on the size of that data, we represent its length using bytes. But how exactly does this encoding work? Let’s break it down step by step.

What is a Byte?

byte is a unit of data that consists of 8 bits. Each bit is a binary value, meaning it can either be a 0 or a 1. For example, if we have a sequence of 8 bits, it might look like this:

00000000

That’s a byte with all zeroes.

How We Encode Lengths Using Bytes

When encoding a length in bytes, we need to consider how large that length is. The larger the number, the more bytes we need to represent it. Let’s look at an example to make this clearer.

Example 1: Length = 255

Let’s say we have the value 255, and we want to encode this length in bytes. Since 255 can fit in 1 byte (8 bits), it’s simple. The binary representation of 255 is:

11111111

This is the maximum value you can store in a single byte.

Now, if we represent this in hexadecimal (which is base-16), 11111111 becomes FF. So, in hexadecimal, 255 is represented as:

FF

When we apply this to our code, we see that for a length of 255, we use a 2-byte encoding because of the condition that values greater than or equal to 128 require extra bytes:

byteArrayOf(0x81.toByte(), 0xFF.toByte())

Here:

  • 0x81.toByte() is a special marker that tells the system we are using 2 bytes.
  • 0xFF.toByte() is the actual length, encoded in a single byte.

Example 2: Length = 256

What if the length is 256? Since 256 is greater than what we can store in 1 byte, we need to use 2 bytes (or 16 bits) to represent this length. The 16-bit binary representation of 256 looks like this:

00000001  - 2nd byte
00000000  - 1st byte

If we convert this to hexadecimal, we get:

01 00

In the code, when we encounter a length of 256, we enter the else condition that handles lengths larger than 255, which requires 3 bytes of encoding:

byteArrayOf(0x82.toByte(), 0x01.toByte(), 0x00.toByte())

Here:

  • 0x82.toByte() is a marker that indicates we’re using 3 bytes for the length.
  • 0x01.toByte() and 0x00.toByte() represent the high and low bytes of the length 256, respectively.

Why Do We Need Different Byte Encodings?

The reason we use different encodings is efficiency. For small lengths (less than 128), we can fit the value into 1 byte, which saves space. As the length grows, we need more bytes to accurately represent the number, but by using markers like 0x81 and 0x82, we help the system understand how many bytes are being used for the length.

Recap of the Implementation

Here is our implementation of the function that converts a length into its corresponding hex string representation, depending on whether it requires 1, 2, or 3 bytes of encoding:


private fun encodeLengthToHexString(length: Int): String {
    val bytes = when {
        length < 128 -> byteArrayOf(length.toByte()) // 1-byte length encoding
        length <= 255 -> byteArrayOf(0x81.toByte(), length.toByte()) // 2-byte length encoding
        else -> byteArrayOf(0x82.toByte(), (length shr 8).toByte(), length.toByte()) // 3-byte length encoding
    }
    
    return bytes.joinToString("") { String.format("%02X", it) }
}

This function takes an integer length as input and:

  • Uses 1 byte if the length is less than 128.
  • Uses 2 bytes if the length is between 128 and 255.
  • Uses 3 bytes for lengths greater than 255.

It then returns the encoded length as a hexadecimal string.

Conclusion

In summary, encoding lengths into bytes depends on the size of the value. Small values can fit in 1 byte, but larger values require 2 or 3 bytes. By using this encoding system, we ensure efficient storage and handling of data lengths, while the system knows exactly how many bytes to expect for any given value.

By understanding how binary and hexadecimal representations work, you can better manage length encodings in your programs and ensure your data is handled correctly.

If you’d like to learn more or get involved in the QCAT ecosystem, feel free to check out my guides on QCAT specification below or check out AF Payments Inc.’s GitHub for the latest updates and contribution guidelines.

Don't forget to subscribe to my blog so you never miss out on my latest guides and content!

DISCLAIMER

The information provided in this article is for informational purposes only and does not constitute professional advice. While efforts have been made to ensure accuracy, AF Payments Inc. reserves the right to update or modify the QCAT standard and related materials at any time. Use of the QCAT standard is subject to the terms of its license agreement, and any implementation must adhere to AFPI’s guidelines and licensing requirements. For the latest details and official documentation, please refer to AF Payments Inc.’s authorized channels.

Comments

Popular posts from this blog

Understanding Number Systems: Decimal, Binary, and Hexadecimal

In everyday life, we use numbers all the time, whether for counting, telling time, or handling money. The number system we’re most familiar with is the   decimal system , but computers use other systems, such as   binary   and   hexadecimal . Let’s break down these number systems to understand how they work. What is a Number System? A number system is a way of representing numbers using a set of symbols and rules. The most common number systems are: Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16) Each system has a different “base” that tells us how many unique digits (symbols) are used to represent numbers. Decimal Number System (Base 10) This is the system we use daily. It has  10 digits , ranging from  0 to 9 . Example: The number  529  in decimal means: 5 × 1⁰² + 2 × 1⁰¹ + 9 × 1⁰⁰ =  500 + 20 + 9 = 529 Each position represents a power of 10, starting from the rightmost digit. Why Base 10? Decimal is base 10 because it has 10 digits...

How to Monetize Your API as an Individual Developer While Hosting on Your Own Server?

In the API economy, cloud services like AWS, Google Cloud, and Azure offer many conveniences, such as scaling and infrastructure management. However, some developers prefer more control and autonomy, opting to host their APIs on personal servers. Whether for cost efficiency, data privacy, or customization, hosting your own API comes with both advantages and challenges. But, even without cloud platforms, there are effective ways to monetize your API. This guide will explore how individual developers can successfully monetize their APIs while hosting them on their own servers. Why Host Your API on Your Own Server? Hosting your own API gives you full control over the infrastructure and potentially lower long-term costs. Here’s why some developers choose this approach: Cost Control : Instead of paying ongoing cloud fees, you may opt for a one-time or lower-cost hosting solution that fits your budget and resource needs. Data Ownership : You have full control over data, which is critical if ...

The Weight of Responsibility: A Developer’s Journey to Balance Passion and Reality

For the past several years, Eddie has been on a steady climb in his career as a developer, but recently, he found himself at a crossroads — caught between the weight of his responsibilities and the desire to pursue his true passions. His journey began with a three-month internship as a web developer, which led to nearly four years in an application developer role. After that, he spent almost a year as a systems associate, managing tasks across systems analysis, quality assurance, and business analysis. Eventually, he returned to full-time software development for another two years before transitioning into more complex roles. For over a year, he worked as a multi-role software developer and database administrator before stepping into his current position as a senior software developer, database administrator, and cloud administrator — occasionally handling security tasks as well. Now, with over 8 years of professional experience, he also leads a small team of developers, which has been...

The Hidden Costs of Overdesign and Bad Practices in API Systems

In software development, simplicity and clarity are often sacrificed in favor of overly complex solutions. While it can be tempting to add more features and intricate designs to ensure robustness, overdesign and poor practices can have significant consequences. They frustrate developers, lead to inefficiencies, increase costs, and put unnecessary strain on system resources.  A recent example involving a team that has faced challenges with complexity highlights the pitfalls of such an approach. Overdesign: The Problem of Too Much Complexity Overdesign occurs when systems are built with more complexity than necessary. This might manifest in bloated APIs, convoluted data flows, or excessive checks and processes that don’t add substantial value. The goal is often to anticipate future problems, but this approach typically results in cumbersome systems that are difficult to maintain and scale. In one case, a company found itself paying a hefty price just to host two API services and a po...

Selenium for Beginners: What, Where, When, and Why to Use It in Automated Testing

In today’s software development landscape, automated testing has become essential for delivering robust applications efficiently. Among various automated testing tools,   Selenium   stands out as one of the most widely used and beginner-friendly options. As you embark on your journey into automated testing, it’s crucial to understand the   what, where, when, and why   of using Selenium. In this guide we will run through these essentials and help you decide if Selenium is the right tool for you. What is Selenium? Selenium  is an open-source framework used primarily for automating web browsers. It enables developers and testers to write scripts that interact with websites, simulating actions like clicking buttons, filling out forms, and navigating pages, which allows for comprehensive automated testing. Selenium supports multiple programming languages, including Python, Java, C#, and JavaScript, making it flexible for teams with different coding preferences. Key C...