r/PowerShell • • 4d ago

Question Byte (Array) to Integer

Good evening,

I'm currently dabbling in ASN1 Encoding and I've run into an annoying issue. It seems I was lucky enough to somehow never have encountered this before:

How can I convert from a byte array to integer without writings logic for all the different cases that can happen

[BitConverter] ::ToInt32() needs at least a byte array with the size of 4. Furthermore, the other way around it will always at least spit out a byte array with size 4 even if the int is in the singlebyte range with the rest zeroed (this is especially annoying in conjunction with my ASN1 practice)

[Convert]::ToInt32 only works with single items but not arrays

ToByte Method needs a format which I can't seem to find anywhere. Google search is pretty much Full of the two convert Classes or some generic IFormat stuff but no list what the formats for the methods are

Is there anything that will give me an int (non array) if the byte array im converting from has only one Value (and vice versa)

Ah an while I'm at it:

Is there a way to set the default output type when doing bit manipulation?

I im at the point where I get more than mildly annoyed if I so much as read "significant bit" anywhere. Gimme unsigned

9 Upvotes

16 comments sorted by

4

u/Ferretau 4d ago

"134".ToByte((get-culture).NumberFormat)

1

u/xXFl1ppyXx 3d ago

Thanks!

Can you tell me where the formats are listed?

I've searched a lot but somehow always ended up at the classes or the Format Operator pages

1

u/Ferretau 3d ago

Use (get-culture).NumberFormat for the current settings for others you need to start digging into the culture settings for other languages.

NumberFormatInfo Class (System.Globalization) | Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberformatinfo?view=net-10.0

Convert.ToByte Method (System) | Microsoft Learn: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tobyte?view=net-10.0#system-convert-tobyte(system-string-system-iformatprovider)

2

u/jmizrahi 4d ago

Yep, Get-Culture or alternatively [cultureinfo]::CurrentCulture.NumberFormat

3

u/Tidder802b 4d ago

Maybe you could provide an example of the inputs and desired outputs?

2

u/xXFl1ppyXx 4d ago

[byte[]]@(127, 0, 0) -> [int]127

Does not work with [BitConverter] and also does not work with [Convert]

5

u/jmizrahi 4d ago edited 4d ago

A 24 bit BE value? If it's always going to be 24 bits/3 bytes, why not just pad it with an extra null byte? For little endian, [byte[]]@(127, 0, 0, 0) or for big endian, [byte[]]@(0, 0, 0, 127) -- both will work with [BitConverter]::ToUInt32. (edit: had big/little examples swapped, big is leading zeroes)

1

u/xXFl1ppyXx 3d ago

Because of the ASN1 Part. Encoding in ASN1 consists of three parts (for ints)

First byte is a tag (2 for ints) second bytes is the byte length followed with the actual bytes for the value

Depending on what ruleset is used, the shortest form per value is mandatory 

So an int with value 1 and 4 bytes is illegal

An int with the value 256 must be encoded in two bytes etc.

In reverse I can encounter a size 3 byte array that I need to convert to int aswell

The actual process going from byte array to int is trivial

I never ran into issues using the convert classes and 3byte integers are quite exotic to say the least but in this particular situation it results in. Problems

I thought that there might be a solution for this particular problem that I'm just not aware of

But If it can't be helped then there's nothing to be done about it and I have to take care of this myself

2

u/jmizrahi 3d ago

Ah that makes a lot more sense. I forgot you were talking about ASN.1 and thought you were talking about literal 24 bit numbers, lol

so, going back to your original questions about bit manipulation and format definitions, I think we need some more context about your project or goal so we can give you advice that is actually useful. earlier you said that you are "dabbling in ASN.1 encoding" but that's extremely vague and could mean anything from "checking my cat's food bowl with an SNMP gauge" all the way to "crashing my telco's 5G core by sending malformed NGAP PDUs" -- are you implementing an ASN.1 parser as an educational exercise or is there a practical reason/goal that you're trying to achieve?

If it's just educational, we can dive extremely deep into the ~trash heap~ rabbit hole that is ITU standardisation and talk about all the quirks and nuances of ASN.1 that will naturally come with implementing a parser in a language that is actively resistant against low level concepts like variable length binary encoding.

on the other hand if you just want to read an X.509 certificate or send an LDAP packet then you should not be manually reading or writing bytes, instead you should be using the System.Formats.Asn1 APIs. Some of them aren't (easily) used in PowerShell because they expect {,ReadOnly}Span<T> types† but the AsnReader and AsnWriter classes are totally usable (interchange byte[] <-> ReadOnlyMemory<byte>)

Here's some example code:

function Get-Asn1Blob {
    $stream = [Formats.Asn1.AsnWriter]::new('DER') # AsnEncodingRules, BER/CER/DER

    # Outer seq
    [void]$stream.PushSequence()

    $stream.WriteInteger(69)
    $stream.WriteInteger(420)
    $stream.WriteInteger(1337)

    $stream.WriteBoolean($true)

    $stream.WriteObjectIdentifier("1.3.6.1.4.1.9.9.155.3.2.3") # {iso(1) identified-organization(3) dod(6) internet(1) private(4) enterprise(1) 9 ciscoMgmt(9) ciscoSdslLineMIB(155) ciscoSdslLineMIBConformance(3) ciscoSdslLineMIBGroups(2) cSdslSturGroup(3)}

    $stream.WriteCharacterString('UTF8String', "test 12345 string foobar") # enum Formats.Asn1.UniversalTagNumber

    [byte[]] $raw = 0x41, 0x42, 0x43, 0x44
    $stream.WriteOctetString($raw)

    # Inner seq
    [void]$stream.PushSequence()
    65530 .. 65539 | foreach { $stream.WriteInteger($_) }
    $stream.PopSequence() # End inner seq

    $stream.PopSequence() # End outer seq

    Write-Output -NoEnumerate $stream.Encode()
}

function Read-Asn1Blob {
    param(
        [byte[]] $blob
    )

    $stream = [System.Formats.Asn1.AsnReader]::new($blob, 'DER')

    # Outer seq
    $scope  = $stream.ReadSequence()
    $object = @{
        'Integers' = (0..2 | foreach { $scope.ReadInteger() })
        'Bool'     = $scope.ReadBoolean()
        'OID'      = $scope.ReadObjectIdentifier()
        'String'   = $scope.ReadCharacterString('UTF8String')
        'Raw'      = [byte[]] $scope.ReadOctetString()
    }

    # Inner seq
    $scope2 = $scope.ReadSequence()
    $object.Add('InnerSeqArray', @( (0..9 | foreach { $scope2.ReadInteger() }) ))
    $scope2.ThrowIfNotEmpty() # End inner seq

    $scope.ThrowIfNotEmpty() # End outer seq

    $stream.ThrowIfNotEmpty() # End stream, no more bytes to parse

    Write-Output ([PSCustomObject] $object)
}

† {,ReadOnly}Spans are ref struct types; ref struct types are strictly stack-allocated which makes them impossible to use in PowerShell (everything in PS is a PSObject and lives on the heap)

1

u/xXFl1ppyXx 2d ago

It's just for educational purposes.

I've already spend time with the asnreader and writer but ive wanted more

I don't plan to go the whole way but knowing about that stuff sure isn't detrimental. Even though it's quite dry, it's interesting enough to me to invest some hours into it

I was quite happy when I first encoded a > 128 oid correctly

But, again, my biggest gripes were with the byte length and there doesn't seem to be an easy way to deal with  that. I'm doing it manually then

3

u/CarrotBusiness2380 4d ago

You could convert it to a [byte] or a [short], but otherwise no. An int is by definition 32 bits / 4 bytes.

If you want unsigned then use [convert]::ToUInt32(...)

1

u/CarrotBusiness2380 4d ago

Here is one way to accomplish your specific request though:

$bytes = [byte[]]@(127, 0, 0)
$output = [UInt32]0

foreach($b in $bytes[($bytes.Length - 1)..0])
{
    $output = $output -shl 8
    $output = $output -bor $b
}

2

u/xXFl1ppyXx 3d ago

As stated in the other post. The conversion itself is not the problem.  the asn part makes the headaches

[BitConverter]::ToUInt32($bytes + [byte[]]::new(4-$bytes.count), 0)

4

u/[deleted] 4d ago

[removed] — view removed comment

1

u/xXFl1ppyXx 3d ago

Im on core 

I'll have a look at that. I'm rarely using bigints so this went over my head. Thanks

2

u/jmizrahi 4d ago

PS7 overloads for new:

[bigint]::new

OverloadDefinitions
-------------------
bigint new(int value)
bigint new(uint value)
bigint new(long value)
bigint new(ulong value)
bigint new(float value)
bigint new(double value)
bigint new(decimal value)
bigint new(byte[] value)