Skip to content

Instantly share code, notes, and snippets.

View okjake's full-sized avatar

Jake Pyne okjake

View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

FWIW: I (@rondy) am not the creator of the content shared here, which is an excerpt from Edmond Lau's book. I simply copied and pasted it from another location and saved it as a personal note, before it gained popularity on news.ycombinator.com. Unfortunately, I cannot recall the exact origin of the original source, nor was I able to find the author's name, so I am can't provide the appropriate credits.


Effective Engineer - Notes

What's an Effective Engineer?

@bgstaal
bgstaal / gist:5665027
Created May 28, 2013 18:37
Float to buffer to string back to float
float f = -0.123456;
size_t n = sizeof(f); //number of bytes in a float
unsigned char buffer[n]; //create buffer of n bytes
memcpy(&buffer, &f, n); // copy underlying bytes from float to buffer
string str((char *)buffer, n); //instantiate new string with contents of buffer
float f2;
memcpy(&f2, str.data(), n); //copy bytes from string data prop to f2
printf("%f\n", f2); // prints -0.123456