简体   繁体   中英

Read a BigInteger serialized from Java into C#

I have a BigInteger serialized to a file by a Java program using the writeObject method from ObjectOutputStream .

Can I deserialize it in C#? I tried using the java.math and java.io classes of vjslib , but I get an exception:

InvalidClassException the class does not match the class of the persisted object for cl = java.lang.Number : __SUID = -8742448824652078965, getSUID(cl) = 3166984097235214156

Any ideas?

Do you have control over the serialization step from Java?

If so, I would suggest serializing a byte array, either as binary, or base64, and reading the byte array from the serialized structure.

Then you can pass the byte array to the System.Numerics.BigInteger constructor.

如果你不介意丑陋的黑客:我会说最简单的(尽管不是最有效的)方法是在Java端将其写成ASCII字符串,并在C#端解析该字符串,而不是使用二进制/序列化。

I suggest you don't use serialization for this, since the two versions of BigInteger are not compatible - they have different version ids.

You should write the object out in some other way, probably using the byte array from BigInteger.toByteArray

Reading this this question about serialization might also be insightful for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM