I want to output binary data to StdOut on Mac/Linux

I want to output binary data to standard output in Xojo’s Console app for Mac/Linux.

Because I want to communicate between the Chrome extension and Xojo using Native Messaging.
The data sent by Native Messaging from xojo to the Chrome extension contains binary data.

I have tried the following code.

Stdout.Write(String.ChrByte(9)  + String.ChrByte(42) + String.ChrByte(42) + String.ChrByte(0) + "{""a"":""b""}")

As a result,
Stdout.Write(String.ChrByte(9) + String.ChrByte(42) + String.ChrByte(42) were sent, “String.ChrByte(0) + “{”“a””:"“b”"}")" were not sent.

I would like to achieve the following code and equating things in xojo of Mac/Linux.

#!/bin/bash
# Loop forever, to deal with chrome.runtime.connectNative
while true; do

    message='{"message": "Hello world!"}'
    messagelen=${#message}

    messagelen1=$(( ($messagelen      ) & 0xFF ))
    messagelen2=$(( ($messagelen >>  8) & 0xFF ))
    messagelen3=$(( ($messagelen >> 16) & 0xFF ))
    messagelen4=$(( ($messagelen >> 24) & 0xFF ))

    # Print the message byte length followed by the actual message.
    printf "$(printf '\\x%x\\x%x\\x%x\\x%x' \
        $messagelen1 $messagelen2 $messagelen3 $messagelen4)%s" "$message"
    sleep 3
done

from https://stackoverflow.com/questions/24764657/how-do-i-use-a-shell-script-as-chrome-native-messaging-host-application

Could you please tell me how to do this?

Have you tried putting the bytes into a memoryblock and writing that to stdout?

Yes
The following code confirms this.
However, data after 0 (=mb.Byte(2) = 0 in code) was not output.

Is there a way for xojo to output binary 0 to stdout?

Var jsonStr As String ="{""a"":""n""}"
jsonStr = jsonStr.DefineEncoding( Encodings.UTF8 )

Var totalSize As Byte = 4 + jsonStr.Bytes+1

Var mb As New MemoryBlock(4 + jsonStr.Bytes+1)
mb.Byte(0) =totalSize
mb.Byte(1) = jsonStr.Length
mb.Byte(2) = 0
mb.Byte(3) = 0
mb.Byte(4) = 0
mb.StringValue(5, jsonStr.Length) = jsonStr


Do Until False
  Print(mb.PString(0))
  App.DoEvents (1000)
Loop

※I ran the above code with the following command
exe_name > hoge.txt

The contents of hoge.txt, expressed in binary form, were as follows
09 09 09 09 09 09 09 09 09 09 09 09 09 …

Binary data is … binary. You can’t just send that to StdOut as is. To send binary data to StdOut you need to send ASCII (printable) characters. To do so, convert every byte to it’s ASCII representation in Hex. This is easy to do. It can be in the form of a Hexdump, see Hex dump - Wikipedia. There can be utilities that would read a binary file and output the Hexdump to a file that you may then print for example.

Google the printf command

%x means hex so it looks like you have to encode each byte.

I know that I can write 0x00 to a file with the following command in a shell script.

printf "\x0" > fuga.bin

Can I do the same thing with xojo (Linux/Mac)?

Xojo has a hex function. From memory, it doesn’t prefix 0-F with a leading zero so you might have to pad it.

You could EncodeBase64 before sending to StdOut.