Gorgon
Show / Hide Table of Contents

Class GorgonNumericFormattingExtension

Extension methods to provide formatting on numeric values for memory and hexadecimal values.

Inheritance
object
GorgonNumericFormattingExtension
Inherited Members
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.Core
Assembly: Gorgon.Core.dll
Syntax
public static class GorgonNumericFormattingExtension

Methods

| Edit this page View Source

FormatHex(byte)

Function to format a byte value into a hexadecimal string.

Declaration
public static string FormatHex(this byte value)
Parameters
Type Name Description
byte value

Byte value to format.

Returns
Type Description
string

The formatted byte value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(short)

Function to format a short value into a hexadecimal string.

Declaration
public static string FormatHex(this short value)
Parameters
Type Name Description
short value

Short value to format.

Returns
Type Description
string

The formatted short value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(int)

Function to format an integer value into a hexadecimal string.

Declaration
public static string FormatHex(this int value)
Parameters
Type Name Description
int value

Integer value to format.

Returns
Type Description
string

The formatted integer value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(long)

Function to format a long value into a hexadecimal string.

Declaration
public static string FormatHex(this long value)
Parameters
Type Name Description
long value

Long value to format.

Returns
Type Description
string

The formatted long value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(nint)

Function to format a pointer (nint) value into a hexadecimal string.

Declaration
public static string FormatHex(this nint pointer)
Parameters
Type Name Description
nint pointer

Pointer to format.

Returns
Type Description
string

The formatted address of the pointer.

Remarks

This will return a string with the number formatted as a hexadecimal number. Like other overloads of this method, this will pad zeroes to the left of the value based on the size of the type, but unlike the other overloads, it will use the correct number of zeroes based on the platform (x64, x86). For example:

nint hexValue = 122388812;

Console.WriteLine(hexValue.FormatHex()); // Produces "074B814C" for x86, and "00000000074B814C" for x64
| Edit this page View Source

FormatHex(ushort)

Function to format an unsigned short value into a hexadecimal string.

Declaration
public static string FormatHex(this ushort value)
Parameters
Type Name Description
ushort value

Unsigned short value to format.

Returns
Type Description
string

The formatted unsigned short value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(uint)

Function to format an unsigned integer value into a hexadecimal string.

Declaration
public static string FormatHex(this uint value)
Parameters
Type Name Description
uint value

Unsigned integer value to format.

Returns
Type Description
string

The formatted unsigned integer value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(ulong)

Function to format an unsigned long value into a hexadecimal string.

Declaration
public static string FormatHex(this ulong value)
Parameters
Type Name Description
ulong value

Unsigned long value to format.

Returns
Type Description
string

The formatted unsigned long value.

Remarks

This will return a string with the number formatted as a hexadecimal number. This number will be padded with zeroes that represent the size of the type. For example:

byte hexValue = 15;

Console.WriteLine(hexValue.FormatHex()); // Produces "0F"

int hexValueInt = 127;

Console.WriteLine(hexValueInt.FormatHex()); // Produces "007F".

short hexValueShort = 1023;

Console.WriteLine(hexValueShort.FormatHex()); // Produces "03FF".
| Edit this page View Source

FormatHex(nuint)

Function to format a pointer (nuint) value into a hexadecimal string.

Declaration
public static string FormatHex(this nuint pointer)
Parameters
Type Name Description
nuint pointer

Pointer to format.

Returns
Type Description
string

The formatted address of the pointer.

Remarks

This will return a string with the number formatted as a hexadecimal number. Like other overloads of this method, this will pad zeroes to the left of the value based on the size of the type, but unlike the other overloads, it will use the correct number of zeroes based on the platform (x64, x86). For example:

nint hexValue = 122388812;

Console.WriteLine(hexValue.FormatHex()); // Produces "074B814C" for x86, and "00000000074B814C" for x64
| Edit this page View Source

FormatMemory(byte)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this byte amount)
Parameters
Type Name Description
byte amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory in bytes.

Remarks

This will produce a string value with the number of bytes, suffixed with the word 'bytes'.

byte bytes = 128;

Console.WriteLine(bytes.FormatMemory());

// Produces: "128 bytes".
| Edit this page View Source

FormatMemory(double)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this double amount)
Parameters
Type Name Description
double amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This overload is like the FormatMemory(int) method, only it will return the value with floating point formatting. e.g: "3.25 MB" instead of "3.0 MB"

| Edit this page View Source

FormatMemory(short)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this short amount)
Parameters
Type Name Description
short amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory in kilobytes.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, or the number of kilobytes suffixed with the abbreviation 'KB' if the value is above 1023.

short bytes = 999;
short kilobytes = 2048;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());

// Produces: "128 bytes" and "2.0 KB".
| Edit this page View Source

FormatMemory(int)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this int amount)
Parameters
Type Name Description
int amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, the number of kilobytes suffixed with the abbreviation 'KB' if the value is within the range of 1024 to 1048575, the number of megabytes suffixed with the abbreviation MB if the value is within the range of 1048576 to 1073741823, or the number of gigabytes with the suffix of 'GB' if the value is greater than 1073741823.

int bytes = 999;
int kilobytes = 2048;
int megabytes = 3145728;
int gigabytes = int.MaxValue;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());
Console.WriteLine(megabytes.FormatMemory());
Console.WriteLine(gigabytes.FormatMemory());

// Produces: "128 bytes", "2.0 KB", "3.0 MB", and "2 GB".

If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.

| Edit this page View Source

FormatMemory(long)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this long amount)
Parameters
Type Name Description
long amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, the number of kilobytes suffixed with the abbreviation 'KB' if the value is within the range of 1024 to 1048575, the number of megabytes suffixed with the abbreviation MB if the value is within the range of 1048576 to 1073741823, the number of gigabytes with the suffix of 'GB' if the value is within the range of 1073741824 to 1099511627775, the number of terabytes with the suffix 'TB' if the value is within the range of 1099511627776 to 1125899906842623, of the number of petabytes with the suffix 'PB' if the value is greater than 1125899906842623.

long bytes = 999;
long kilobytes = 2048;
long megabytes = 3145728;
long gigabytes = 4294967296;
long terabytes = 5497558138880;
long petabytes = 6755399441055744;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());
Console.WriteLine(megabytes.FormatMemory());
Console.WriteLine(gigabytes.FormatMemory());
Console.WriteLine(terabytes.FormatMemory());
Console.WriteLine(petabytes.FormatMemory());

// Produces: "128 bytes", "2.0 KB", "3.0 MB", "4.0 GB", "5.0 TB", and "6.0 PB".

If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.

| Edit this page View Source

FormatMemory(float)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this float amount)
Parameters
Type Name Description
float amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This overload is like the FormatMemory(int) method, only it will return the value with floating point formatting. e.g: "3.25 MB" instead of "3.0 MB"

| Edit this page View Source

FormatMemory(ushort)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this ushort amount)
Parameters
Type Name Description
ushort amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, or the number of kilobytes suffixed with the abbreviation 'KB' if the value is above 1023.

ushort bytes = 999;
ushort kilobytes = 2048;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());

// Produces: "128 bytes" and "2.0 KB".

If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.

| Edit this page View Source

FormatMemory(uint)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this uint amount)
Parameters
Type Name Description
uint amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, the number of kilobytes suffixed with the abbreviation 'KB' if the value is within the range of 1024 to 1048575, the number of megabytes suffixed with the abbreviation MB if the value is within the range of 1048576 to 1073741823, or the number of gigabytes with the suffix of 'GB' if the value is greater than 1073741823.

uint bytes = 999;
uint kilobytes = 2048;
uint megabytes = 3145728;
uint gigabytes = 3221225472;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());
Console.WriteLine(megabytes.FormatMemory());
Console.WriteLine(gigabytes.FormatMemory());

// Produces: "128 bytes", "2.0 KB", "3.0 MB", and "3 GB".

If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.

| Edit this page View Source

FormatMemory(ulong)

Function to return a formatted string containing the memory amount.

Declaration
public static string FormatMemory(this ulong amount)
Parameters
Type Name Description
ulong amount

Amount of memory in bytes to format.

Returns
Type Description
string

A string containing the formatted amount of memory.

Remarks

This will produce a string value with the number of bytes suffixed with the word 'bytes' if less than 1023, the number of kilobytes suffixed with the abbreviation 'KB' if the value is within the range of 1024 to 1048575, the number of megabytes suffixed with the abbreviation MB if the value is within the range of 1048576 to 1073741823, the number of gigabytes with the suffix of 'GB' if the value is within the range of 1073741824 to 1099511627775, the number of terabytes with the suffix 'TB' if the value is within the range of 1099511627776 to 1125899906842623, of the number of petabytes with the suffix 'PB' if the value is greater than 1125899906842623.

ulong bytes = 999;
ulong kilobytes = 2048;
ulong megabytes = 3145728;
ulong gigabytes = 4294967296;
ulong terabytes = 5497558138880;
ulong petabytes = 6755399441055744;

Console.WriteLine(bytes.FormatMemory());
Console.WriteLine(kilobytes.FormatMemory());
Console.WriteLine(megabytes.FormatMemory());
Console.WriteLine(gigabytes.FormatMemory());
Console.WriteLine(terabytes.FormatMemory());
Console.WriteLine(petabytes.FormatMemory());

// Produces: "128 bytes", "2.0 KB", "3.0 MB", "4.0 GB", "5.0 TB", and "6.0 PB".

If the value cannot be represented with a string suffix, then the number of bytes will be displayed as default.

  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author