Skip to content

Instantly share code, notes, and snippets.

@gravejester
Created February 19, 2016 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gravejester/c370e2cbc70d0a1b9993 to your computer and use it in GitHub Desktop.
Save gravejester/c370e2cbc70d0a1b9993 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.UInt64
.LINK
https://communary.wordpress.com/
https://github.com/gravejester/Communary.ToolBox
.NOTES
Author: Øyvind Kallstad
Date: 09.05.2015
Version: 1.0
#>
[OutputType([System.Byte[]])]
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[uint64]$Number
)
$n = $Number
$numberOfDigits = 1 + [convert]::ToUInt64([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