2013-10-11 38 views
0

我想下面的Python代码转换为C#转换蟒蛇struct.pack到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); 

任何想法如何解决它?谢谢!

回答

0

所以事实证明,struct.pack输出3个额外的空字符,结果搞砸了。

这里是工作的代码:

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

丑陋,但做这项工作:)