Endianness data storage

I was originally looking at generating random numbers in PowerShell by using the .NET class [System.Security.Cryptography.RandomNumberGenerator]. At some point I got sidetracked and wondered how two and four byte numbers were stored in my byte array. So hence the following code to look at at things.

function Show-Endian {
[CmdletBinding()]
[OutputType([System.Void])]
param()

  # true if the architecture is little-endian;
  # false if it is big-endian.
  $endi = '';
  if ([System.BitConverter]::IsLittleEndian) {
      $endi = 'little endian';
  } else {
      $endi = 'big endian';
  }
  Write-Output ('This computer appears to be {0}' -f $endi);
}

function Part-Two {
[CmdletBinding()]
[OutputType([System.Void])]
param()

  [System.Byte]$my8 = 0;       # Represents an 8-bit unsigned integer.
  [System.UInt16]$my16 = 0;    # Represents a 16-bit unsigned integer.
  [System.UInt32]$my32 = 2041; # Represents a 32-bit unsigned integer.

  $data = New-Object Byte[] 4;
  $data.Clear();

  Write-Output '';
  $mynum = $my32;

  Write-Output 'Unsigned bytes';
  Write-Output ('Maximum value: {0:X2}' -f [System.Byte]::MaxValue);
  Write-Output ('Minimum value: {0:X2}' -f [System.Byte]::MinValue);

  Write-Output '';
  Write-Output ('mynum is now decimal {0:N0}, hex {1:X2}' -f $mynum,$mynum);
  Write-Output ('data has size of {0} bytes' -f $data.Length);

  # convert the number in variable 'mynum' to the array
  # 'data'.
  $data = [System.BitConverter]::GetBytes($mynum);
  Write-Output '';
  Write-Output 'Looking at positions in the array'
  for ($i = 0; $i -lt $data.Length; $i++) {
      Write-Output ('Position {0} = {1:X2}' -f $i,$data[$i]);
  }

 }

$png = [System.Security.Cryptography.RandomNumberGenerator]::Create();
# -----
Show-Endian;
Part-Two;
# -----
$png.Dispose();
Write-Output 'End of test';

 

By running the script, we can see what’s happening.


This computer appears to be little endian

Unsigned bytes
Maximum value: FF
Minimum value: 00

mynum is now decimal 2,041, hex 7F9
data has size of 4 bytes

Looking at positions in the array
Position 0 = F9
Position 1 = 07
Position 2 = 00
Position 3 = 00
End of test

Hex 7F9 is stored in two bytes of our four byte array. F9 is stored in $data[0], and the remaining number, 7, is stored in $data[1]. Of course, for our test we could have used a two byte array to store the number used in our example.

 

The Wikipedia article at https://en.wikipedia.org/wiki/Endianness gives a good understanding of big-endian and little-endian.

 

Keywords: big-endian, little-endian, Endianness, byte, bitconverter

 

 

Advertisements
This entry was posted in powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s