Low level dual value implementation in Java and C #

I face a tough problem. I want to read data from sqlite database using C #. The data field in the database is of type blob. This field contains double values ​​(also has other types of values, such as text and int) written by Java. I need to read double values ​​using C #.

I'm sorry that I made a terrible mistake in the question. I read double values ​​in Java, creating a ByteBuffer initialized with a blob stream that is read from the database. And there is no ByteBuffer ByteBuffer data structure in C #, so I use the following code to get a double value from a stream of 8 bytes.

public double getDouble() { if(CURRENT_POSITION + 7 >= CURRENT_LENGTH) { return 0; } double ret = (double)(TEMP_BYTE_ARRAY[CURRENT_POSITION + 7] << 56 | TEMP_BYTE_ARRAY[CURRENT_POSITION + 6] << 48 | TEMP_BYTE_ARRAY[CURRENT_POSITION + 5] << 40 | ..... ); return ret; } 

But I cannot get the correct double value from the blob field using C # using the same code. Can someone give me some tips on how to convert a binary binary style value defined by Java to a C # double value?

By the way, since the blob field compressed many other types of values, I cannot change the database structure. Thanks to everyone in advance.

+4
source share
1 answer

Use BitConverter :

 double ret = BitConverter.ToDouble(TEMP_BYTE_ARRAY, CURRENT_POSITION); 

The code you sent just creates an integer and converts the integer to double, which you don't need.

Alternitavely , you can get the same functionality as ByteBuffer by creating a BinaryReader over a MemoryStream . For instance:

 MemoryStream memStream = new MemoryStream(TEMP_BYTE_ARRAY); BinaryReader reader = new BinaryReader(memStream); //read data double a = reader.ReadDouble(); int b = reader.ReadInt(); string c = reader.ReadString(); 
+1
source

Source: https://habr.com/ru/post/1439624/


All Articles