Output binary data on PowerShell pipeline

后端 未结 4 1616
独厮守ぢ
独厮守ぢ 2021-02-13 20:05

I need to pipe some data to a program\'s stdin.

  1. First 4 bytes are a 32 bit unsigned integer representing the length of the data. These 4 bytes are exactly the sam
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-13 20:53

    Would this work for you?

    $fileName = "C:\test.txt"
    $data = [IO.File]::ReadAllText($fileName)
    $prefix = ([BitConverter]::GetBytes($data.Length) | foreach-object {
      "\x{0:X2}" -f $_
    }) -join ""
    "{0}{1}" -f $prefix,$data
    

    You can replace "\x{0:X2}" -f $_ with $_ -as [Char] if you want $prefix to contain the raw data representations of the bytes.

提交回复
热议问题