从Java编写的C#中读取字节数组 [英] read byte array from C# that is written from Java
问题描述
我正在尝试从C#编写一个Integer并从Java读取它.两种语言的整数均为4字节.但是,当我从C#编写时,以下字节中写入了整数11000.表示第一个字节为1,其余为0.
I am trying to write an Integer from C# and read it from Java. An integer is 4 bytes in both languages. However when I write it from C#, integer 1 is written in the following bytes 1000. Meaning the first byte is 1 and the rest is 0.
但是在Java中,相同的东西写为0001.测量的前3个字节为0,最后一个为1.
But in Java the same thing is written as 0001. Meaing the first 3 bytes are 0 and the last is 1.
在这些语言之间是否有一种简单的读写方法,而不是手动反转每个4字节?Java代码
Is there a simple way of reading and writing among these languages instead of manually reversing every 4 byte? The code for Java
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putInt(1);
for(byte b: buffer.array()){
System.out.print(b);
}
C#的代码
MemoryStream ms = new MemoryStream();
using(BinaryWriter writer = new BinaryWriter(ms))
{
writer.Write((int)1);
}
foreach(byte b in ms.ToArray()){
Console.Write(b);
}
推荐答案
As this post points aus http://kirkwylie.blogspot.com/2008/11/c-binarywriter-is-little-endian-because.html C# BinaryWriter supports only little endian so you have to configure it on the java site with the order method http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#order%28java.nio.ByteOrder%29
这篇关于从Java编写的C#中读取字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!