0

次のpythonコードをC#に変換しようとしています

sys.stdout.write(struct.pack('I', len(message)))
sys.stdout.write(message)
sys.stdout.flush()

コンソールに出力するには、C# プログラムが必要です。次のことを試してみましたが、C# と python プログラムは同じものを出力しません - struct.pack 部分が台無しになっているようです。

Stream stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)message.Length);
Console.Write(message);

それを解決する方法はありますか?ありがとう!

4

1 に答える 1

0

したがって、struct.pack が 3 つの追加の null 文字を出力し、結果が台無しになることが判明しました。

動作したコードは次のとおりです。

Stream stdout = Console.OpenStandardOutput();
stdout.WriteByte((byte)message.Length);
stdout.WriteByte((byte)'\0');
stdout.WriteByte((byte)'\0');
stdout.WriteByte((byte)'\0');
Console.Write(message);

醜いですが、仕事をします:)

于 2013-10-11T01:11:50.930 に答える