c# convert.tobyte java实现同样效果,将C#字节转换为Java字节

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

C# code:

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);

When I read on java I do:

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();

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

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;

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;

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

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

SOLVED: the values above were printed wrongly (during the wrong time), because of that I thought there were problems with the code.

It prints the corrects values bellow:

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

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();

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.

解决方案

I solved the problem.

The problem is that: there wasn't any problem with my code.

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

I didn't know very well C# I posted this question on on stackoverflow.

Maybe this simple example can be useful for other people.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值