Skip to content

Instantly share code, notes, and snippets.

@gravejester
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gravejester/95e95f2af0f28e4d3453 to your computer and use it in GitHub Desktop.
Save gravejester/95e95f2af0f28e4d3453 to your computer and use it in GitHub Desktop.
function ConvertTo-Digits {
<#
.SYNOPSIS
Convert an integer into an array of bytes of its individual digits.
.DESCRIPTION
Convert an integer into an array of bytes of its individual digits.
.EXAMPLE
ConvertTo-Digits 145
.INPUTS
System.Int32
.NOTES
Author: Øyvind Kallstad
Date: 09.05.2015
Version: 1.0
#>
[OutputType([System.Byte[]])]
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[ValidateRange(1, [int]::MaxValue)]
[int]$Number
)
$n = $Number
$numberOfDigits = 1 + [convert]::ToInt32([math]::Floor(([math]::Log10($n))))
$digits = New-Object Byte[] $numberOfDigits
for ($i = ($numberOfDigits - 1); $i -ge 0; $i--) {
$digit = $n % 10
$digits[$i] = $digit
$n = [math]::Floor($n / 10)
}
Write-Output $digits
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment