Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* char = 1 byte */
- typedef unsigned char byte;
- /* for third method */
- union {
- float f;
- byte b[4];
- } c;
- int main(){
- /* 35.34 = b01000010000011010101110000101001 IEE754 */
- /* 35.34 = 0x420d5c29 IEE754 */
- byte bytes[4];
- /* MSB */
- bytes[3] = 0x42;
- bytes[2] = 0x0d;
- bytes[1] = 0x5c;
- bytes[0] = 0x29;
- /* normal bitshift method */
- unsigned int a1 = bytes[0]<<24;
- a1 |= (bytes[1]<<16);
- a1 |= (bytes[2]<<8);
- a1 |= bytes[3];
- float a = (float)a1;
- /* memcpy method */
- float b = 0;
- memcpy(&b, &bytes, sizeof(b));
- /* union */
- c.b[3] = bytes[3];
- c.b[2] = bytes[2];
- c.b[1] = bytes[1];
- c.b[0] = bytes[0];
- printf("a = %f\nb = %f\nc = %f\n",a,b,c.f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement