PS array of Big Endian words.

AnthonyGuyon 1 Reputation point
2021-11-08T07:56:19.603+00:00

For my own edification I am attempting to convert a java function into .net (PowerShell). The problem is the function barfs before anything is produced and I am wondering what needs to be changed to make it produce the intended Big Endian word array, from a raw string input.

This is the Java function.

function convert(input)
{
  var output = Array(input.length >> 2);
  for(var i = 0; i < output.length; i++)

    output[i] = 0;

  for(var i = 0; i < input.length * 8; i += 8)
    output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
  return output;
}

This is my PowerShell port of the function above, which as I say doesn't.

function convert($inp)
{
    $s = ($inp.Length) -shr 2
    $output = [system.collections.arraylist]::new($s)
    for($i = 0; $i -le $output.Capacity; $i++)
    {
        $output[$i] = 0
        for($i = 0; $i -lt ($inp.length * 8); $i += 8)
        {
            $output[$i -shr 5] -ne (([byte][char]$inp[($i/8)]) -band 0xFF) -shl (24 - $i % 32)
        }
    }
    return $output
}

I am looking for a PowerShell code level solution only.

Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-11-08T16:31:21.183+00:00

    How about this?

    https://www.powershellgallery.com/packages/Convert/0.2.0.5/Content/Public%5CConvertFrom-StringToByteArray.ps1

    $string = "abc"
    "Unicode format"
    $string | format-hex -Encoding Unicode
    $bytes = ConvertFrom-StringToByteArray -String $string -Encoding BigEndianUnicode 
    ""
    "BigEndian format"
    $bytes | format-hex 
    

    Produces...

    Unicode format
    
    
               00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
    
    00000000   61 00 62 00 63 00                                a.b.c.          
    
    BigEndian format
    
    
               Path:  
    
               00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
    
    00000000   00 61 00 62 00 63                                .a.b.c          
    
    
    
    PS C:\> 
    

  2. Rich Matheisen 47,901 Reputation points
    2021-12-24T21:04:09.21+00:00

    If you can convince PowerShell to give you chunks of memory in raw format, you could create, say, unsigned integers of the appropriate number of bits, and reverse the bytes.

    [UInt64]$uvalue = 0x0102030405060708;
    
    [byte[]]$bytes = [System.BitConverter]::GetBytes($uvalue)
    [byte[]]$rbytes = @()
    # reverse the bytes (sorry, but there's not "Reverse" method for a byte array)
    for ($i = $bytes.GetUpperBound(0); $i -ge $bytes.GetLowerBound(0); --$i){
        $rbytes += $bytes[$i]
    }
    
    $ convert the reversed bytes into the original type
    $to = "To" + $uvalue.GetType()
    $swapped  = [System.BitConverter]::$to($rbytes, 0)
    "{0:X}" -f $swapped
    

    The value displayed will have the leading zero suppressed. :-(


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.