Transmit Float as Byte Stream Using Union

Code

#include <stdio.h>
#include <stdint.h>

typedef union {
    float f;
    uint8_t bytes[4];
} FloatPacket;

int main() {
    float input;
    uint8_t exchanged[4];
    scanf("%f", &input);
    
    FloatPacket Packet;
    Packet.f = input;
    /* Print original bytes (LSB first) */
    /* Print original bytes */
    for (int i = 0; i < 4; i++) {
        printf("Byte %d: %u\n", i, Packet.bytes[i]);
    }
   
    /* Byte exchange (Little Endian -> Big Endian) */
   // exchanged[0] = Packet.bytes[0];
   // exchanged[1] = Packet.bytes[1];
   // exchanged[2] = Packet.bytes[2];
   // exchanged[3] = Packet.bytes[3];

    /* Print exchanged bytes */
//for (int i = 0; i < 4; i++) {
//printf("Byte %d: %u\n", i, exchanged[i]);
  //  }

    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

1

Expected Output

Byte 0: 0 Byte 1: 0 Byte 2: 128 Byte 3: 63