.NET HELP

ByteSize C# (How it Works For Developers)

Published December 24, 2023
Share:

Introduction

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.

int number = 42; 
byte [] byteArray = number.ToByteArray(); // makes byte size representation
int number = 42; 
byte [] byteArray = number.ToByteArray(); // makes byte size representation
Dim number As Integer = 42
Dim byteArray() As Byte = number.ToByteArray() ' makes byte size representation
VB   C#

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.

byte value = 0b00001111; 
bool isBitSet = value.IsBitSet(3);
byte value = 0b00001111; 
bool isBitSet = value.IsBitSet(3);
Dim value As Byte = &B00001111
Dim isBitSet As Boolean = value.IsBitSet(3)
VB   C#

1.3. Mastering Endianess

Endianess 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.

byte [] data = new byte [] { 0x01, 0x02, 0x03 }; 
uint crc32 = data.CalculateCRC32();
byte [] data = new byte [] { 0x01, 0x02, 0x03 }; 
uint crc32 = data.CalculateCRC32();
Dim data() As Byte = { &H1, &H2, &H3 }
Dim crc32 As UInteger = data.CalculateCRC32()
VB   C#

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
  2. Embark on Byte Adventures:

    using Bytesize;
    int number = 42;
    byte [] byteArray = number.ToByteArray();
    byte value = 0b00001111;
    bool isBitSet = value.IsBitSet(3);
    byte [] data = new byte [] { 0x01, 0x02, 0x03 };
    uint crc32 = data.CalculateCRC32();
    using Bytesize;
    int number = 42;
    byte [] byteArray = number.ToByteArray();
    byte value = 0b00001111;
    bool isBitSet = value.IsBitSet(3);
    byte [] data = new byte [] { 0x01, 0x02, 0x03 };
    uint crc32 = data.CalculateCRC32();
    Imports Bytesize
    Private number As Integer = 42
    Private byteArray() As Byte = number.ToByteArray()
    Private value As Byte = &B00001111
    Private isBitSet As Boolean = value.IsBitSet(3)
    Private data() As Byte = { &H1, &H2, &H3 }
    Private crc32 As UInteger = data.CalculateCRC32()
    VB   C#

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 ToByteArray, IsBitSet, and CalculateCRC32, are likely provided by the ByteSize library 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
  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.ToString("KB")}");
            Console.WriteLine($"File Size in KiB: {bs.ToString("KiB")}");
            Console.WriteLine($"File Size in Bytes: {bs.ToString("B")}");
            Console.WriteLine($"File Size in bits: {bs.ToString("b")}");
        }
    }
    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.ToString("KB")}");
            Console.WriteLine($"File Size in KiB: {bs.ToString("KiB")}");
            Console.WriteLine($"File Size in Bytes: {bs.ToString("B")}");
            Console.WriteLine($"File Size in bits: {bs.ToString("b")}");
        }
    }
    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.ToString("KB")}")
    		Console.WriteLine($"File Size in KiB: {bs.ToString("KiB")}")
    		Console.WriteLine($"File Size in Bytes: {bs.ToString("B")}")
    		Console.WriteLine($"File Size in bits: {bs.ToString("b")}")
    	End Sub
    End Class
    VB   C#

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.

< PREVIOUS
SevenZip C# (How it Works For Developers)
NEXT >
NET 8.0 (How it Works For Developers)

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,781,565 View Licenses >