【发布时间】:2013-10-21 04:33:16
【问题描述】:
我没有意识到我的代码有什么问题,请看一下:
C# 代码:
const int MOVE = 112;
MemoryStream m = new MemoryStream();
m.SetLength(4 + 1 + (1 + 1 + 1 + 1));
BinaryWriter bw = new BinaryWriter(m);
int id_ = getId();
bw.Write(Converter.GetBigEndian(id_));
sbyte eventMove = MOVE;
sbyte rowFromByte = 4;
sbyte colFromByte = 2;
sbyte rowToByte = 1;
sbyte colToByte = 3;
bw.Write(eventMove);
bw.Write(rowFromByte);
bw.Write(colFromByte);
bw.Write(rowToByte);
bw.Write(colToByte);
当我阅读 java 时,我会这样做:
ByteBuffer msg = an instance of ByteBuffer...
int id = msg.getInt();
byte event = msg.get();
byte rowFrom = msg.get();
byte colFrom = msg.get();
byte rowTo = msg.get();
byte colTo = msg.get();
每次我在 java 上阅读它都会到达直到事件正常。
有时它可以正常工作,读取一切正常,例如如果我使用 c#:
sbyte rowFromByte = 12;
sbyte colFromByte = 2;
sbyte rowToByte = 1;
sbyte colToByte = 3;
它可以工作,但是如果我在第一 rowFromByte 上输入一个值
sbyte rowFromByte = 4;
sbyte colFromByte = 2;
sbyte rowToByte = 1;
sbyte colToByte = 3;
java 代码没有正确读取它读取的值:
rowFrom: -7, colFrom: 0, rowTo: -7, colTo: 0
已解决:上面的值打印错误(在错误的时间),因为我认为代码有问题。 它在下面打印正确的值:
rowFrom: 4, colFrom: 2, rowTo: 1, colTo: 3
在 c# 上,我使用 Memorystream 获取要发送到 java 客户端的字节:
MemoryStream message = the instance of memory stream...
bytesMessage = message.ToArray();
为什么将值
有一些非常奇怪或愚蠢的事情我没有涉及。
【问题讨论】:
-
您可能需要指定
msg的类型 -
好的,ByteBuffer 我已经编辑过了。
-
流是如何传输到Java进程的?通过文件系统、网络或其他方式?
-
我从 C# 通过 NetworkStream & TcpClient 传输
标签: c# java type-conversion byte endianness