将C#字节转换为Java字节 [英] Converting c# byte to java byte

查看:686
本文介绍了将C#字节转换为Java字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有意识到我的代码有什么问题,请看一下:

I'm not realizing what is wrong with my code, take a look:

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上阅读时,直到事件确定为止。

Every time I read on java it reaches till the event ok.

有时它可以正常工作,并且可以读取所有内容,例如,如果我戴上c#:

Sometimes it works correctly, reads everything ok e.g if I put on c#:

sbyte rowFromByte = 12;
sbyte colFromByte = 2;
sbyte rowToByte = 1;
sbyte colToByte = 3;

它可以工作,但是如果我在第一个rowFromByte上输入值<== 7,例如:

It works, but if I put a value <= 7 on the first rowFromByte e.g:

sbyte rowFromByte = 4;
sbyte colFromByte = 2;
sbyte rowToByte = 1;
sbyte colToByte = 3;

Java代码未正确读取其读取的值:

the java code doesn't read the values correctly it reads:

rowFrom: -7, colFrom: 0, rowTo: -7, colTo: 0

已解决:上面的值打印有误(在错误的时间),因为我认为代码有问题。
打印以下正确的值:

 rowFrom: 4, colFrom: 2, rowTo: 1, colTo: 3

在c#上,我使用Memorystream来获取要发送到Java客户端的字节: / p>

On c# I use the Memorystream to get the bytes to send to java client:

MemoryStream message = the instance of memory stream...
bytesMessage = message.ToArray();

为什么将row< = 7的值放在Java上会返回错误的字节关联?

WHY putting a value <= 7 for rowFrom returns wrong bytes association on java?

有些东西我很奇怪或愚蠢。

There's something very weird or silly I'm not getting into.

推荐答案

我解决了问题。

问题是:我的代码没有任何问题

我以为这是一个愚蠢的东西:我在服务器上打印了错误的变量。

As I supposed this was a Silly stuff: I was printing the wrong variables on server.

我不太清楚C#我张贴了这个关于stackoverflow的问题。
也许这个简单的示例可能对其他人有用。

I didn't know very well C# I posted this question on on stackoverflow. Maybe this simple example can be useful for other people.

这篇关于将C#字节转换为Java字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆