Skip to footer content
.NET HELP

ByteSize C# (How it Works For Developers)

In the dynamic and continually evolving realm of software development, the adept handling of binary data at the byte-size level is an indispensable requirement for human-readable string and integer math. Package ByteSize, an exceptionally resilient and versatile C# library, arises as an influential companion for developers aiming to optimize and augment their byte size-centric operations using the extension method. Boasting an extensive array of features and removing ambiguity, the new ByteSize facilitates the simplification of intricate byte file size-handling tasks, rendering them not only more straightforward but also remarkably efficient human-readable string representation and making byte size representation.

A gigabyte will be translated into kilobytes and bits will be converted to megabytes for ByteSize representation. We want 1.59 MB in KBs and 1226311 MB in bits. We build ByteSize structs by using C# ByteSize Gigabytes. These values are returned to the database by executing the ToString utility class method. We also use ByteSize bits and ToString method as representations in MB.

In this article, we will use the ByteSize C# Library with IronPDF Library for string representation.

1. Unveiling the Power of ByteSize

1.1. Byte Conversion Magic

ByteSize transforms the intricate process of converting various data types into byte arrays into an effortless endeavor. Developers can now seamlessly bridge the gap between numerical byte sizes and values and non-numerical data types with succinct and expressive methods with decimal places as shown in the example below.

// Example demonstrating conversion of an integer to a byte array
int number = 42; 
byte[] byteArray = BitConverter.GetBytes(number); // Converts the integer to a byte array
// Example demonstrating conversion of an integer to a byte array
int number = 42; 
byte[] byteArray = BitConverter.GetBytes(number); // Converts the integer to a byte array
' Example demonstrating conversion of an integer to a byte array
Dim number As Integer = 42
Dim byteArray() As Byte = BitConverter.GetBytes(number) ' Converts the integer to a byte array
$vbLabelText   $csharpLabel

1.2. Bitwise Brilliance

Dealing with individual bits within a byte is often an intricate dance. ByteSize elegantly simplifies this chore, offering developers clear and expressive methods for bitwise operations.

// Example to check if a specific bit is set
byte value = 0b00001111; 
bool isBitSet = (value & (1 << 3)) != 0; // Checks if the 4th bit is set
// Example to check if a specific bit is set
byte value = 0b00001111; 
bool isBitSet = (value & (1 << 3)) != 0; // Checks if the 4th bit is set
' Example to check if a specific bit is set
Dim value As Byte = &B00001111
Dim isBitSet As Boolean = (value And (1 << 3)) <> 0 ' Checks if the 4th bit is set
$vbLabelText   $csharpLabel

1.3. Mastering Endianess

Endian format intricacies can lead to subtle bugs in byte-oriented code. ByteSize, however, by default acts as a seasoned guide, providing support for handling different endian formats. This ensures a seamless conversion process between different endian representations.

// Example of calculating CRC32 for byte data
byte[] data = new byte[] { 0x01, 0x02, 0x03 }; 
uint crc32 = Crc32Algorithm.Compute(data); // Calculates CRC32 checksum
// Example of calculating CRC32 for byte data
byte[] data = new byte[] { 0x01, 0x02, 0x03 }; 
uint crc32 = Crc32Algorithm.Compute(data); // Calculates CRC32 checksum
' Example of calculating CRC32 for byte data
Dim data() As Byte = { &H1, &H2, &H3 }
Dim crc32 As UInteger = Crc32Algorithm.Compute(data) ' Calculates CRC32 checksum
$vbLabelText   $csharpLabel

1.4. Checksums and Hashing Made Simple

Ensuring data integrity and security is paramount. ByteSize simplifies the calculation of common checksums and hashes, for example, offering methods for widely used algorithms like CRC32 and MD5.

1.5. Mastery over Byte Arrays

Byte array manipulations become a breeze with ByteSize. It provides streamlined operations for appending, concatenating, and slicing byte arrays, empowering developers to manipulate double-size binary data with precision.

1.6. Base64 Brilliance

Base64 string encoding and decoding, often a crucial aspect of data handling, is made seamless. ByteSize provides simple methods and code easier than before for converting byte arrays to and from Base64 strings.

2. Embracing ByteSize in Your Project

Integrating ByteSize into your C# project is a straightforward journey

  1. Install the ByteSize NuGet Package:

    Install-Package ByteSize
    Install-Package ByteSize
    SHELL
  2. Embark on Byte Adventures:

    using ByteSizeLib; // Example using ByteSizeLib
    int number = 42;
    byte[] byteArray = BitConverter.GetBytes(number); // Converts integer to byte array
    byte value = 0b00001111;
    bool isBitSet = (value & (1 << 3)) != 0; // Checks if the 4th bit is set
    byte[] data = new byte[] { 0x01, 0x02, 0x03 };
    uint crc32 = Crc32Algorithm.Compute(data); // CRC32 checksum calculation
    using ByteSizeLib; // Example using ByteSizeLib
    int number = 42;
    byte[] byteArray = BitConverter.GetBytes(number); // Converts integer to byte array
    byte value = 0b00001111;
    bool isBitSet = (value & (1 << 3)) != 0; // Checks if the 4th bit is set
    byte[] data = new byte[] { 0x01, 0x02, 0x03 };
    uint crc32 = Crc32Algorithm.Compute(data); // CRC32 checksum calculation
    Imports ByteSizeLib ' Example using ByteSizeLib
    Private number As Integer = 42
    Private byteArray() As Byte = BitConverter.GetBytes(number) ' Converts integer to byte array
    Private value As Byte = &B00001111
    Private isBitSet As Boolean = (value And (1 << 3)) <> 0 ' Checks if the 4th bit is set
    Private data() As Byte = { &H1, &H2, &H3 }
    Private crc32 As UInteger = Crc32Algorithm.Compute(data) ' CRC32 checksum calculation
    $vbLabelText   $csharpLabel

This C# code snippet uses the ByteSize library for byte-level operations. It converts the integer 42 into a byte array, checks if the third bit is set in a byte represented as 0b00001111, and calculates the CRC32 checksum for a byte array { 0x01, 0x02, 0x03 }. The specific methods, such as BitConverter.GetBytes and bitwise operations, are standard in C# for efficient byte manipulation.

3. IronPDF C# Library

IronPDF is a powerful and versatile C# library designed to revolutionize the way developers work with PDFs in their applications. Whether you're creating, manipulating, or extracting content from PDF documents, IronPDF provides a comprehensive set of tools and functionalities that streamline the entire process. With its intuitive API and extensive documentation, developers can effortlessly integrate advanced PDF capabilities into their C# applications, empowering them to generate high-quality PDFs, add annotations, handle digital signatures, and much more.

IronPDF's robust features, combined with its commitment to simplicity and efficiency, make it a go-to solution for developers seeking to enhance their C# projects with seamless PDF handling and output. In this era where digital document management is pivotal, IronPDF emerges as an indispensable asset, offering unparalleled ease of use and flexibility for all PDF-related tasks in C# development.

4. Steps to Integrate ByteSize with IronPDF

  1. Install IronPDF

    Simply run the command below to install IronPDF.

    Install-Package IronPdf
    Install-Package IronPdf
    SHELL
  2. Use ByteSize for PDF Manipulation:

    using IronPdf;
    using ByteSizeLib;
    using System;
    using System.IO;
    
    class Program
    {
        static void Main()
        {
            // Create a simple PDF document using IronPDF
            var renderer = new ChromePdfRenderer();
            // Create a PDF from an HTML string using C#
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
            // Save the IronPDF document to a file using string filename
            pdf.SaveAs("output.pdf");
    
            // Use ByteSizeLib to get file information
            var fileInfo = new FileInfo("output.pdf");
            var fileSize = fileInfo.Length;
            ByteSize bs = ByteSize.FromBytes(fileSize);
    
            // Print information about the file size
            Console.WriteLine($"File Size: {bs}");
            Console.WriteLine($"File Size in KB: {bs.Kilobytes}");
            Console.WriteLine($"File Size in KiB: {bs.KibiBytes}");
            Console.WriteLine($"File Size in Bytes: {bs.Bytes}");
            Console.WriteLine($"File Size in bits: {bs.Bits}");
        }
    }
    using IronPdf;
    using ByteSizeLib;
    using System;
    using System.IO;
    
    class Program
    {
        static void Main()
        {
            // Create a simple PDF document using IronPDF
            var renderer = new ChromePdfRenderer();
            // Create a PDF from an HTML string using C#
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
            // Save the IronPDF document to a file using string filename
            pdf.SaveAs("output.pdf");
    
            // Use ByteSizeLib to get file information
            var fileInfo = new FileInfo("output.pdf");
            var fileSize = fileInfo.Length;
            ByteSize bs = ByteSize.FromBytes(fileSize);
    
            // Print information about the file size
            Console.WriteLine($"File Size: {bs}");
            Console.WriteLine($"File Size in KB: {bs.Kilobytes}");
            Console.WriteLine($"File Size in KiB: {bs.KibiBytes}");
            Console.WriteLine($"File Size in Bytes: {bs.Bytes}");
            Console.WriteLine($"File Size in bits: {bs.Bits}");
        }
    }
    Imports IronPdf
    Imports ByteSizeLib
    Imports System
    Imports System.IO
    
    Friend Class Program
    	Shared Sub Main()
    		' Create a simple PDF document using IronPDF
    		Dim renderer = New ChromePdfRenderer()
    		' Create a PDF from an HTML string using C#
    		Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
    		' Save the IronPDF document to a file using string filename
    		pdf.SaveAs("output.pdf")
    
    		' Use ByteSizeLib to get file information
    		Dim fileInfo As New FileInfo("output.pdf")
    		Dim fileSize = fileInfo.Length
    		Dim bs As ByteSize = ByteSize.FromBytes(fileSize)
    
    		' Print information about the file size
    		Console.WriteLine($"File Size: {bs}")
    		Console.WriteLine($"File Size in KB: {bs.Kilobytes}")
    		Console.WriteLine($"File Size in KiB: {bs.KibiBytes}")
    		Console.WriteLine($"File Size in Bytes: {bs.Bytes}")
    		Console.WriteLine($"File Size in bits: {bs.Bits}")
    	End Sub
    End Class
    $vbLabelText   $csharpLabel

This C# program utilizes the IronPDF library to create a basic PDF document using the ChromePdfRenderer. The content of the PDF is generated from an HTML string ("<h1>Hello World</h1>"). The resulting PDF output is then saved to a file named "output.PDF." The ByteSizeLib library is employed to obtain information about the file size of the generated PDF, and various metrics such as kilobytes, kibibytes, bytes, and bits are printed to the console for informational purposes. Overall, the code showcases the integration of IronPDF for PDF generation and ByteSizeLib for convenient file size representation.

ByteSize C# (How It Works For Developers) Figure 1 - Output

5. Conclusion

The integration of ByteSize and IronPDF libraries in C# equips developers with a powerful toolkit for efficient byte-level operations and seamless PDF generation and manipulation. ByteSize offers a wealth of features, including a long-byte extension method for simplifying tasks such as byte conversion, bitwise operations, endianness handling, checksums, and byte array manipulations. It also facilitates easy integration of a double value for precise numerical representation. IronPDF, on the other hand, emerges as a robust solution for handling PDFs in C#, providing an intuitive API for creating and manipulating PDF documents effortlessly.

The provided C# code exemplifies this integration by utilizing IronPDF to generate a PDF document and ByteSize to retrieve and display file size information in various formats. This combination showcases the adaptability and synergy of these libraries, making them valuable assets for developers seeking efficient and comprehensive solutions in their C# projects. Whether managing binary data or handling PDF documents, ByteSize's long extension method and IronPDF collectively contribute to a streamlined and effective development experience.

IronPDF offers a free trial license that is a great opportunity for users to get to know its functionality. An HTML to PDF tutorial using IronPDF can be found in our HTML to PDF Tutorial.

Frequently Asked Questions

What is ByteSize C#?

ByteSize is a versatile C# library designed to simplify byte size-centric operations, allowing developers to perform conversions and manipulations of binary data efficiently.

How does ByteSize simplify byte conversions?

ByteSize transforms the complex process of converting various data types into byte arrays into an easy task by providing succinct and expressive methods for developers.

What are the main features of ByteSize?

ByteSize offers features for byte conversion, bitwise operations, handling different endian formats, calculating checksums, manipulating byte arrays, and Base64 encoding/decoding.

How can I integrate ByteSize into my C# project?

To integrate ByteSize, install the ByteSize NuGet package using the command 'Install-Package ByteSize' and use its library to perform byte-level operations in your project.

What is a library for PDF document manipulation?

IronPDF is a powerful C# library for creating, manipulating, and extracting content from PDF documents, offering an intuitive API and extensive functionalities for developers.

How do byte size operations and PDF manipulation work together?

ByteSize can be used alongside IronPDF to handle file size representations of PDFs created or manipulated with IronPDF, allowing for efficient byte-level operations and file size calculations.

How do I install a library for PDF manipulation in my project?

Install IronPDF by running the command 'Install-Package IronPdf' in your project, allowing you to utilize its PDF generation and manipulation capabilities.

Can ByteSize handle endianess in C#?

Yes, ByteSize provides support for handling different endian formats, ensuring seamless conversion processes between various endian representations.

What examples of byte operations can ByteSize perform?

ByteSize can convert integers to byte arrays, check specific bits, compute CRC32 checksums, and perform Base64 encoding/decoding, among other byte operations.

Is there a free trial for PDF manipulation libraries?

Yes, IronPDF offers a free trial license, providing an opportunity for users to explore its functionality before committing to a purchase.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.