Skip to content

Instantly share code, notes, and snippets.

@alemures
Last active September 15, 2016 10:58
Show Gist options
  • Save alemures/d429210a90ab37949c52b0a14dd4a8f0 to your computer and use it in GitHub Desktop.
Save alemures/d429210a90ab37949c52b0a14dd4a8f0 to your computer and use it in GitHub Desktop.
Convert a double to long in ANSI C (Java's doubleToRawLongBits)
// Make sure that unsigned long is 8 bytes of length
unsigned long utilDoubleToLong(double value) {
unsigned long longValue;
memcpy(&longValue, &value, sizeof(unsigned long));
return longValue;
}
double utilLongToDouble(unsigned long value) {
double doubleValue;
memcpy(&doubleValue, &value, sizeof(unsigned long));
return doubleValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment