1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
// Example program
#include <iostream>
#include <memory>
#include <stdio.h>
#include <string.h>
struct RGB8 {
uint8_t G;
};
struct RGB24 {
uint8_t R;
uint8_t G;
uint8_t B;
};
struct RGBA {
uint8_t R;
uint8_t G;
uint8_t B;
uint8_t A;
};
int main() {
uint8_t * buffer=(uint8_t*)malloc(100);
uint8_t values[100]={0x63,0x61,0x74,0x73,0x64,0x6f,0x67,0x73}; //catsdogs
memcpy(buffer, &values, 100);
fprintf(stderr,"Buffer values\n");
for (uint32_t i = 0; i < 8; ++i) {
fprintf(stderr, "\\%02x", (unsigned char)*(buffer+i));
}
fprintf(stderr,"\n");
fprintf(stderr,"Buffer values stepped one byte at a time\n");
RGB8 *rgb8=(RGB8*)buffer;
for (uint32_t i = 0; i < 8; ++i) {
fprintf(stderr, "\\%02x", (unsigned char)(rgb8+i)->G);
fprintf(stderr," ");
}
fprintf(stderr,"\n");
fprintf(stderr,"Buffer values stepped three bytes at a time\n");
RGB24 *rgb24=(RGB24*)buffer;
for (uint32_t i = 0; i < 2; ++i) {
fprintf(stderr, "\\%02x", (unsigned char)(rgb24+i)->R);
fprintf(stderr, "\\%02x", (unsigned char)(rgb24+i)->G);
fprintf(stderr, "\\%02x", (unsigned char)(rgb24+i)->B);
fprintf(stderr," ");
}
fprintf(stderr,"\n");
fprintf(stderr,"Buffer values stepped four bytes at a time\n");
RGBA *rgba=(RGBA*)buffer;
for (uint32_t i = 0; i < 2; ++i) {
fprintf(stderr, "\\%02x", (unsigned char)(rgba+i)->R);
fprintf(stderr, "\\%02x", (unsigned char)(rgba+i)->G);
fprintf(stderr, "\\%02x", (unsigned char)(rgba+i)->B);
fprintf(stderr, "\\%02x", (unsigned char)(rgba+i)->A);
fprintf(stderr," ");
}
fprintf(stderr,"\n");
free(buffer);
}
|