r/PowerShell • • 19h 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

8 Upvotes

13 comments sorted by

5

u/skiThooly 18h ago

are you on Windows PowerShell 5.1 or PS 7? Newer .NET versions added methods to BigInteger that let you specify isUnsigned and isBigEndian directly in the constructor, which solves pretty much every annoying quirk with ASN1 integer encoding

1

u/xXFl1ppyXx 1h 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 17h 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)

4

u/Ferretau 17h ago

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

1

u/xXFl1ppyXx 1h 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

2

u/jmizrahi 17h ago

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

3

u/Tidder802b 19h ago

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

2

u/xXFl1ppyXx 19h ago

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

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

5

u/jmizrahi 17h ago edited 17h 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 1h 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

3

u/CarrotBusiness2380 19h 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 19h 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 1h 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)