我在同一个程序的两个版本之间遇到“兼容性”问题,第一个是用Java编写的,第二个是C#中的一个端口。
我的目标是将一些数据写入文件(例如,在Java中),就像一系列数字一样,然后能够在C#中读取它。显然,操作应该按相反的顺序工作。
例如,我想按顺序编写3个数字,用以下模式表示:
所以,我可以按以下顺序放置一个新文件:2(作为字节),120(作为int32),180(作为int32)
在Java中,写作过程或多或少都是这样的:
FileOutputStream outputStream;
byte[] byteToWrite;
// ... initialization....
// first byte
outputStream.write(first_byte);
// integers
byteToWrite = ByteBuffer.allocate(4).putInt(first_integer).array();
outputStream.write(byteToWrite);
byteToWrite = ByteBuffer.allocate(4).putInt(second_integer).array();
outputStream.write(byteToWrite);
outputStream.close();
阅读部分如下:
FileInputStream inputStream;
ByteBuffer byteToRead;
// ... initialization....
// first byte
first_byte = inputStream.read();
// integers
byteToRead = ByteBuffer.allocate(4);
inputStream.read(byteToRead.array());
first_integer = byteToRead.getInt();
byteToRead = ByteBuffer.allocate(4);
inputStream.read(byteToRead.array());
second_integer = byteToRead.getInt();
inputStream.close();
C#代码如下。写作:
FileStream fs;
byte[] byteToWrite;
// ... initialization....
// first byte
byteToWrite = new byte[1];
byteToWrite[0] = first_byte;
fs.Write(byteToWrite, 0, byteToWrite.Length);
// integers
byteToWrite = BitConverter.GetBytes(first_integer);
fs.Write(byteToWrite, 0, byteToWrite.Length);
byteToWrite = BitConverter.GetBytes(second_integer);
fs.Write(byteToWrite, 0, byteToWrite.Length);
读:
FileStream fs;
byte[] byteToWrite;
// ... initialization....
// first byte
byte[] firstByteBuff = new byte[1];
fs.Read(firstByteBuff, 0, firstByteBuff.Length);
first_byte = firstByteBuff[0];
// integers
byteToRead = new byte[4 * 2];
fs.Read(byteToRead, 0, byteToRead.Length);
first_integer = BitConverter.ToInt32(byteToRead, 0);
second_integer = BitConverter.ToInt32(byteToRead, 4);
请注意,当程序的相同Java / C#版本写入和读取文件时,这两个过程都有效。问题是当我尝试从C#版本读取Java程序编写的文件时,反之亦然。 Readed整数总是“奇怪”的数字(如-1451020 ......)。
与C#相比,Java存储和读取32位整数值的方式肯定存在兼容性问题(总是signed
,对吗?)。如何处理?
答案 0 :(得分:2)
这只是一个字母序列问题。您可以使用我的MiscUtil library从.NET读取大端数据。
但是,我强烈建议对Java和.NET采用更简单的方法:
DataInputStream
和DataOutputStream
。没有必要使ByteBuffer
等变得复杂。EndianBinaryReader
,其扩展BinaryReader
(同样EndianBinaryWriter
扩展为BinaryWriter
)或者,请考虑仅使用文字。
答案 1 :(得分:0)
我会考虑使用XML或JSON等标准格式来存储您的数据。然后,您可以在Java和C#中使用标准序列化程序来读取/写入文件。通过这种方法,您可以轻松命名数据字段,从多种语言中读取数据,如果有人在文本编辑器中打开文件,则可以轻松理解,并且可以更轻松地添加要序列化的数据。
E.g。您可以使用Gson in Java和Json.NET in C#来读/写JSON。该类可能在C#中看起来像这样:
public class MyData
{
public byte FirstValue { get; set; }
public int SecondValue { get; set; }
public int ThirdValue { get; set; }
}
// serialize to string example
var myData = new MyData { FirstValue = 2, SecondValue = 5, ThirdValue = -1 };
string serialized = JsonConvert.SerializeObject(myData);
它将序列化为
{"FirstValue":2,"SecondValue":5,"ThirdValue":-1}
同样,Java也很简单。您可以找到如何在每个库中读/写文件的示例。
或者,如果数组是您数据的更好模型:
string serialized = JsonConvert.SerializeObject(new[] { 2, 5, -1 }); // [2,5,-1]