Created
August 16, 2014 01:35
-
-
Save caot/3b80a430c3155cd0032f to your computer and use it in GitHub Desktop.
byte array from / to double [ c++ vs java ]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c++ | |
long result = getByte() & 0xFF; | |
result |= (getByte() & 0xFF) << 8; | |
result |= (getByte() & 0xFF) << 16; | |
result |= (getByte() & 0xFF) << 24; | |
result |= (getByte() & 0xFF) << 32; | |
result |= (getByte() & 0xFF) << 40; | |
result |= (getByte() & 0xFF) << 48; | |
result |= (getByte() & 0xFF) << 56; | |
---- | |
long i64 = result; | |
double m_value; | |
memcpy(&m_value, &i64, sizeof(long)); | |
java: | |
byte[] ba = new byte[8]; | |
for (int i = 0; i < ba.length; i++) | |
ba[i] = (byte) getByte(); | |
byte[] i64 = ba; | |
ByteBuffer buf = ByteBuffer.allocate(8).put(i64); | |
buf.flip(); | |
buf.order(buf.order() == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN); | |
double m_value = buf.getDouble(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment