Skip to content

Instantly share code, notes, and snippets.

@marcorentap
Last active September 26, 2023 20:30
Show Gist options
  • Save marcorentap/7b93fd5a9144d29cbb8c40a5406c309e to your computer and use it in GitHub Desktop.
Save marcorentap/7b93fd5a9144d29cbb8c40a5406c309e to your computer and use it in GitHub Desktop.
#include <stdio.h>
typedef struct {
short short1;
short short2;
} some_struct;
int main() {
// In memory: 48 65 6c 6c 6f 00 48 69 00
char *bytes = "\x48\x65\x6c\x6c\x6f\x00\x48\x69\x00";
long *longVal = (long*) bytes;
int *intVal = (int*) bytes;
short *shortVal = (short*) bytes;
char *charVal = (char*) bytes;
some_struct *structVal = (some_struct*) bytes;
// Remember endianness…
printf("string: %s\n", bytes);
printf("string: %s\n", bytes+6);
printf("long: %lx\n", *longVal);
printf("int: %x\n", *intVal);
printf("short: %x\n", *shortVal);
printf("char: %x\n", *charVal);
printf("struct short1: %x\n", structVal->short1);
printf("struct short1: %x\n", structVal->short2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment