问题
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?
回答1:
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.
回答2:
If you don't mind ugly hacks: I'd say the easiest (albeit not most efficient) way would be to just write it out as an ASCII String on the Java side, and parse that string on the C# side, instead of using binary de/serialization.
回答3:
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
来源:https://stackoverflow.com/questions/4411199/read-a-biginteger-serialized-from-java-into-c-sharp