Method: Pwsh::Manager#length_prefixed_string

Defined in:
lib/pwsh.rb

#length_prefixed_string(data) ⇒ Object

Take a given string and prefix it with a 4-byte length and encode for sending to the PowerShell manager. Data format is:

4 bytes - Little Endian encoded 32-bit integer length of string
          Intel CPUs are little endian, hence the .NET Framework typically is

variable length - UTF8 encoded string bytes

@return A binary encoded string prefixed with a 4-byte length identifier



489
490
491
492
493
# File 'lib/pwsh.rb', line 489

def length_prefixed_string(data)
  msg = data.encode(Encoding::UTF_8)
  # https://ruby-doc.org/core-1.9.3/Array.html#method-i-pack
  [msg.bytes.length].pack('V') + msg.force_encoding(Encoding::BINARY)
end