Skip to content

Reading float as hex #1

@sudhackar

Description

@sudhackar

For

## Lets Confirm it through program
```
#include <stdio.h>
void main()
{
float f;
f = 13.37;
printf("address of f : %p\n", &f);
}
```
### In GDB
```
address of f : 0x7fffffffdda4
...
pwndbg> x/xw 0x7fffffffdda4
0x7fffffffdda4: 0x4155eb85
```

Instead of using gdb to print it as hex another option is to use it as such

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

typedef union {
    uint32_t integer;
    float decimal;
} pack;

int main(int argc, char **argv) {
    pack me;
    me.decimal = 13.37;
    printf("0x%x\n", me.integer);
    return 0;
}

or

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

int main(int argc, char **argv) {
    uint32_t *int_ptr;
    float *float_ptr = (float *)malloc(sizeof(float));
    int_ptr = float_ptr;
    *float_ptr = 13.37;
    printf("0x%x\n", *int_ptr);
    free(float_ptr);
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions