Related
I'm working on an operating system, and I recently switched to VESA Graphics for more pixels and colors to work with. I'm trying to draw text to screen, though no matter what color value I put in, I can't see anything show up. I know there isn't an issue with the bitmap because I've used it before, and I can't see any issues with the VESA Graphics setup. Before, I was using VGA Graphics, and all I had to do for that was input an integer value corresponding to a color table online for 8-bit color. Here I'll show some of my attempts:
static void render(int c0, int c1) {
draw_rect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT, 0); //black?
draw_string("Hello, reader. This is written text.", 100, 180, 0xffffff); //white?
draw_string("If this is displayed, my code works.", 100+c0, 100+c1, 16777215); //white?
}
In the end, all this uses the screen_set function shown here:
void screen_set(u32 color,int x,int y) {
u32 physical_address = modeInfo->PhysBasePtr;
physical_address += y * modeInfo->LinBytesPerScanLine;
physical_address += x * bytesPerPixel;
_sbuffers[physical_address] = color;
}
As seen above, I've tried a few different forms of color input, though nothing seems to work. Here I'll leave my kernel.c and a link to my GitHub:
/*
TODO:
-Filesystem
-Mouse Inputs
-Simple GUI
-3d Viewer
-Maybe Link Downloader and Viewer
*/
#define NULL ((void*)0)
#define true 1
#define false 0
typedef unsigned char uint8_t;
typedef unsigned char u8;
typedef unsigned short uint16_t;
typedef unsigned int u32;
typedef u32 size_t;
typedef unsigned long phys_bytes;
#define SCREEN_WIDTH (int)(modeInfo->XResolution)
#define SCREEN_HEIGHT (int)(modeInfo->YResolution)
#define BPP 32
#define SCREEN_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT)
#define FPS 30
#define PIT_HERTZ 1193131.666
#define CLOCK_HIT (int)(PIT_HERTZ/FPS)
#define KEY_LEFT 0x4B
#define KEY_UP 0x48
#define KEY_RIGHT 0x4D
#define KEY_DOWN 0x50
void * malloc(size_t len) {
static size_t curoff = 0;
static char space[1 * 1024 * 1024 * 1024];
void *ptr = NULL;
if ((curoff + len) < sizeof(space)) {
ptr = &space[curoff]; curoff += len;
} return ptr;
}
typedef struct VBE_modeInfo{
/* Mandatory information for all VBE revisions */
uint16_t ModeAttributes;
uint8_t WinAAttributes;
uint8_t WinBAttributes;
uint16_t WinGranularity;
uint16_t WinSize;
uint16_t WinASegment;
uint16_t WinBSegment;
phys_bytes WinFuncPtr;
uint16_t BytesPerScanLine;
/* Mandatory information for VBE 1.2 and above */
uint16_t XResolution;
uint16_t YResolution;
uint8_t XCharSize;
uint8_t YCharSize;
uint8_t NumberOfPlanes;
uint8_t BitsPerPixel;
uint8_t NumberOfBanks;
uint8_t MemoryModel;
uint8_t BankSize;
uint8_t NumberOfImagePages;
uint8_t Reserved1;
/* Direct Color fields (required for direct/6 and YUV/7 memory models) */
uint8_t RedMaskSize; /* size of direct color red mask in bits */
uint8_t RedFieldPosition; /* bit position of lsb of red mask */
uint8_t GreenMaskSize; /* size of direct color green mask in bits */
uint8_t GreenFieldPosition; /* bit position of lsb of green mask */
uint8_t BlueMaskSize; /* size of direct color blue mask in bits */
uint8_t BlueFieldPosition; /* bit position of lsb of blue mask */
uint8_t RsvdMaskSize; /* size of direct color reserved mask in bits */
uint8_t RsvdFieldPosition; /* bit position of lsb of reserved mask */
uint8_t DirectColorModeInfo; /* direct color mode attributes */
/* Mandatory information for VBE 2.0 and above */
phys_bytes PhysBasePtr;
uint8_t Reserved2[4];
uint8_t Reserved3[2];
/* Mandatory information for VBE 3.0 and above */
uint16_t LinBytesPerScanLine; /* bytes per scan line for linear modes */
uint8_t BnkNumberOfImagePages; /* number of images for banked modes */
uint8_t LinNumberOfImagePages; /* number of images for linear modes */
uint8_t LinRedMaskSize; /* size of direct color red mask (linear modes) */
uint8_t LinRedFieldPosition; /* bit position of lsb of red mask (linear modes) */
uint8_t LinGreenMaskSize; /* size of direct color green mask (linear modes) */
uint8_t LinGreenFieldPosition; /* bit position of lsb of green mask (linear modes) */
uint8_t LinBlueMaskSize; /* size of direct color blue mask (linear modes) */
uint8_t LinBlueFieldPosition; /* bit position of lsb of blue mask (linear modes ) */
uint8_t LinRsvdMaskSize; /* size of direct color reserved mask (linear modes) */
uint8_t LinRsvdFieldPosition; /* bit position of lsb of reserved mask (linear modes) */
u32 MaxPixelClock; /* maximum pixel clock (in Hz) for graphics mode */
uint8_t Reserved4[190]; /* remainder of ModeInfoBlock */
} VBE_modeInfo;
struct VBE_modeInfo *modeInfoPointer;
#define modeInfo modeInfoPointer
#define BUFFER ((u32 *) (modeInfo->PhysBasePtr))
// double buffers
u32 *_sbuffers;
u32 _sback = 0;
static inline void outb(uint16_t port, uint8_t val)
{
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
}
#define bytesPerPixel ((modeInfo->BitsPerPixel + 7) / 8)
void screen_set(u32 color,int x,int y) {
u32 physical_address = modeInfo->PhysBasePtr;
physical_address += y * modeInfo->LinBytesPerScanLine;
physical_address += x * bytesPerPixel;
_sbuffers[physical_address] = color;
}
static inline uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %1, %0"
: "=a"(ret)
: "Nd"(port) );
return ret;
}
const unsigned char font[128-32][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0020 (space)
{ 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00}, // U+0021 (!)
{ 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0022 (")
{ 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00}, // U+0023 (#)
{ 0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00}, // U+0024 ($)
{ 0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00}, // U+0025 (%)
{ 0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00}, // U+0026 (&)
{ 0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0027 (')
{ 0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00}, // U+0028 (()
{ 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00}, // U+0029 ())
{ 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00}, // U+002A (*)
{ 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00}, // U+002B (+)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+002C (,)
{ 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00}, // U+002D (-)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+002E (.)
{ 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00}, // U+002F (/)
{ 0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // U+0030 (0)
{ 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // U+0031 (1)
{ 0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // U+0032 (2)
{ 0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // U+0033 (3)
{ 0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // U+0034 (4)
{ 0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // U+0035 (5)
{ 0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // U+0036 (6)
{ 0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // U+0037 (7)
{ 0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // U+0038 (8)
{ 0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00}, // U+0039 (9)
{ 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+003A (:)
{ 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+003B (;)
{ 0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00}, // U+003C (<)
{ 0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00}, // U+003D (=)
{ 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00}, // U+003E (>)
{ 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00}, // U+003F (?)
{ 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00}, // U+0040 (#)
{ 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00}, // U+0041 (A)
{ 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00}, // U+0042 (B)
{ 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00}, // U+0043 (C)
{ 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00}, // U+0044 (D)
{ 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00}, // U+0045 (E)
{ 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00}, // U+0046 (F)
{ 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00}, // U+0047 (G)
{ 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00}, // U+0048 (H)
{ 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0049 (I)
{ 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00}, // U+004A (J)
{ 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00}, // U+004B (K)
{ 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00}, // U+004C (L)
{ 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00}, // U+004D (M)
{ 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00}, // U+004E (N)
{ 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00}, // U+004F (O)
{ 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00}, // U+0050 (P)
{ 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00}, // U+0051 (Q)
{ 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00}, // U+0052 (R)
{ 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00}, // U+0053 (S)
{ 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0054 (T)
{ 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00}, // U+0055 (U)
{ 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0056 (V)
{ 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00}, // U+0057 (W)
{ 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00}, // U+0058 (X)
{ 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00}, // U+0059 (Y)
{ 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00}, // U+005A (Z)
{ 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00}, // U+005B ([)
{ 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00}, // U+005C (\)
{ 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00}, // U+005D (])
{ 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00}, // U+005E (^)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, // U+005F (_)
{ 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0060 (`)
{ 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00}, // U+0061 (a)
{ 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00}, // U+0062 (b)
{ 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00}, // U+0063 (c)
{ 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00}, // U+0064 (d)
{ 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00}, // U+0065 (e)
{ 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00}, // U+0066 (f)
{ 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0067 (g)
{ 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00}, // U+0068 (h)
{ 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0069 (i)
{ 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E}, // U+006A (j)
{ 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00}, // U+006B (k)
{ 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+006C (l)
{ 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00}, // U+006D (m)
{ 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00}, // U+006E (n)
{ 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00}, // U+006F (o)
{ 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F}, // U+0070 (p)
{ 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78}, // U+0071 (q)
{ 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00}, // U+0072 (r)
{ 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00}, // U+0073 (s)
{ 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00}, // U+0074 (t)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00}, // U+0075 (u)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0076 (v)
{ 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00}, // U+0077 (w)
{ 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00}, // U+0078 (x)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0079 (y)
{ 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00}, // U+007A (z)
{ 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00}, // U+007B ({)
{ 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00}, // U+007C (|)
{ 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00}, // U+007D (})
{ 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+007E (~)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // U+007F
};
static inline void *memcpy(void *dst, const void *src, size_t n)
{
u8 *d = (u8*)dst;
const u8 *s = (const u8*)src;
while (n-- > 0) {
*d++ = *s++;
}
return d;
}
void screen_swap() {
memcpy(BUFFER, _sbuffers, SCREEN_SIZE);
}
unsigned read_pit(void) {
unsigned count = 0;
// al = channel in bits 6 and 7, remaining bits clear
outb(0x43,0b0000000);
count = inb(0x40); // Low byte
count |= inb(0x40)<<8; // High byte
return count;
}
void draw_char(char c, int x, int y, u32 color)
{
const unsigned char *glyph = font[(int)c-32];
for(int cy=0;cy<8;cy++){
for(int cx=0;cx<8;cx++){
if(((int)glyph[cy]&(1<<cx))==(1<<cx)){
screen_set(color,x+cx,y+cy);
}
}
}
}
void draw_string(const char * s, int x, int y, u32 color) {
int i = 0;
while(s[i] != false) {
draw_char(s[i],x+(i*8),y,color);
i++;
}
}
void draw_rect(int pos_x, int pos_y, int w, int h, u32 color) {
for(int y = 0; y<h; y++) {
for(int x = 0; x<w; x++) {
screen_set(color,x+pos_x,y+pos_y);
}
}
}
static void render(int c0, int c1) {
draw_rect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT, 0);
draw_string("Hello, reader. This is written text.", 100, 180, 0xffffff);
draw_string("If this is displayed, my code works.", 100+c0, 100+c1, 16777215);
}
void main(struct VBE_modeInfo *vbe) {
modeInfoPointer = vbe;
_sbuffers = malloc(sizeof(u32) * SCREEN_SIZE);
int clock = 0;
int incC1 = 0;
int incC0 = 0;
while(true) {
uint16_t scancode = (uint16_t) inb(0x60);
clock++;
if(read_pit()!= 0 && clock == CLOCK_HIT) {
if(scancode == KEY_LEFT) {
incC0--;
}else if(scancode == KEY_RIGHT) {
incC0++;
}
if(scancode == KEY_DOWN) {
incC1++;
}else if(scancode == KEY_UP) {
incC1--;
}
clock = 0;
render(incC0,incC1);
screen_swap();
}
}
return;
}
EDIT:
As per request, here is the code for drawing a pixel in VGA Graphics Mode:
void draw_pixel(int pos_x, int pos_y, unsigned char colour)
{
unsigned char* location = (unsigned char*)0xA0000 + 320 * pos_y + pos_x;
*location = colour;
}
I am making a GBA game using the devkitARM toolchain, and I would like to start using sprites. I am using a very simple tutorial, and it tells me to use gfx2gba to convert my bitmaps to C arrays. Here is my bitmap:
In case anything went wrong, I then process the array and convert it back into an image, but something corrupts the image or something like that:
By the way, I may have changed the color palette while converting it back, but that isn't the main thing.
What am I doing wrong? My gfx2gba command is gfx2gba -fsrc -c16 -t8 bitmap.bmp. Here is the generated C file:
//
// numbers (288 uncompressed bytes)
//
extern const unsigned char numbers_Bitmap[288] = {
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x20, 0x22, 0x22, 0x02, 0x20, 0x22, 0x22, 0x22, 0x20, 0x22,
0x22, 0x22, 0x20, 0x22, 0x22, 0x22, 0x20, 0x22, 0x22, 0x02, 0x00, 0x22, 0x22, 0x22, 0x22, 0x22,
0x33, 0x33, 0x33, 0x33, 0x33, 0x03, 0x30, 0x33, 0x33, 0x30, 0x03, 0x33, 0x33, 0x33, 0x03, 0x33,
0x33, 0x03, 0x30, 0x33, 0x33, 0x30, 0x33, 0x33, 0x33, 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x33,
0x99, 0x99, 0x99, 0x99, 0x99, 0x09, 0x90, 0x99, 0x99, 0x90, 0x09, 0x99, 0x99, 0x99, 0x09, 0x99,
0x99, 0x09, 0x90, 0x99, 0x99, 0x99, 0x09, 0x99, 0x99, 0x90, 0x09, 0x99, 0x99, 0x09, 0x90, 0x99,
0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x10, 0x11, 0x11, 0x10, 0x10, 0x11, 0x01, 0x11, 0x10, 0x11,
0x01, 0x00, 0x00, 0x10, 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x11, 0x11,
0x44, 0x44, 0x44, 0x44, 0x44, 0x00, 0x00, 0x44, 0x44, 0x40, 0x44, 0x44, 0x44, 0x00, 0x40, 0x44,
0x44, 0x44, 0x04, 0x44, 0x44, 0x44, 0x04, 0x44, 0x44, 0x00, 0x40, 0x44, 0x44, 0x44, 0x44, 0x44,
0x77, 0x77, 0x77, 0x77, 0x77, 0x07, 0x70, 0x77, 0x77, 0x70, 0x77, 0x77, 0x77, 0x00, 0x70, 0x77,
0x77, 0x70, 0x07, 0x77, 0x77, 0x70, 0x07, 0x77, 0x77, 0x07, 0x70, 0x77, 0x77, 0x77, 0x77, 0x77,
0x55, 0x55, 0x55, 0x55, 0x55, 0x00, 0x00, 0x55, 0x55, 0x55, 0x05, 0x55, 0x55, 0x55, 0x50, 0x55,
0x55, 0x55, 0x50, 0x55, 0x55, 0x05, 0x55, 0x55, 0x55, 0x05, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
0x66, 0x66, 0x66, 0x66, 0x66, 0x06, 0x60, 0x66, 0x66, 0x60, 0x06, 0x66, 0x66, 0x60, 0x06, 0x66,
0x66, 0x06, 0x60, 0x66, 0x66, 0x60, 0x06, 0x66, 0x66, 0x60, 0x06, 0x66, 0x66, 0x06, 0x60, 0x66,
0x88, 0x88, 0x88, 0x88, 0x88, 0x08, 0x80, 0x88, 0x88, 0x80, 0x08, 0x88, 0x88, 0x80, 0x08, 0x88,
0x88, 0x08, 0x00, 0x88, 0x88, 0x88, 0x08, 0x88, 0x88, 0x08, 0x80, 0x88, 0x88, 0x88, 0x88, 0x88
};
And here's the palette:
extern const unsigned short master_Palette[16] = {
0x0000, 0x0390, 0x209c, 0x221c, 0x2304, 0x6108, 0x6114, 0x6280,
0x629c, 0x03ff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
};
EDIT: If anyone wanted to see the code, here is my Python script for converting back into an image:
a = [
2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,0,2,2,0,2,2,2,2,2,2,2,0,2,2,
2,2,2,2,2,0,2,2,2,2,2,2,2,0,2,2,2,2,0,2,0,0,2,2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,3,0,3,3,0,3,3,3,3,3,0,0,3,3,3,3,3,3,3,0,3,3,3,
3,3,0,3,3,0,3,3,3,3,3,0,3,3,3,3,3,3,0,0,0,0,3,3,3,3,3,3,3,3,3,3,
9,9,9,9,9,9,9,9,9,9,0,9,9,0,9,9,9,9,9,0,0,9,9,9,9,9,9,9,0,9,9,9,
9,9,0,9,9,0,9,9,9,9,9,9,0,9,9,9,9,9,9,0,0,9,9,9,9,9,0,9,9,0,9,9,
1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,0,1,1,0,1,1,1,1,0,1,1,
0,1,0,0,0,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,
4,4,4,4,4,4,4,4,4,4,0,0,0,0,4,4,4,4,4,0,4,4,4,4,4,4,0,0,4,0,4,4,
4,4,4,4,0,4,4,4,4,4,4,4,0,4,4,4,4,4,0,0,4,0,4,4,4,4,4,4,4,4,4,4,
7,7,7,7,7,7,7,7,7,7,0,7,7,0,7,7,7,7,7,0,7,7,7,7,7,7,0,0,7,0,7,7,
7,7,7,0,0,7,7,7,7,7,7,0,0,7,7,7,7,7,0,7,7,0,7,7,7,7,7,7,7,7,7,7,
5,5,5,5,5,5,5,5,5,5,0,0,0,0,5,5,5,5,5,5,0,5,5,5,5,5,5,5,5,0,5,5,
5,5,5,5,5,0,5,5,5,5,0,5,5,5,5,5,5,5,0,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,0,6,6,0,6,6,6,6,6,0,0,6,6,6,6,6,6,0,0,6,6,6,
6,6,0,6,6,0,6,6,6,6,6,0,0,6,6,6,6,6,6,0,0,6,6,6,6,6,0,6,6,0,6,6,
8,8,8,8,8,8,8,8,8,8,0,8,8,0,8,8,8,8,8,0,0,8,8,8,8,8,8,0,0,8,8,8,
8,8,0,8,0,0,8,8,8,8,8,8,0,8,8,8,8,8,0,8,8,0,8,8,8,8,8,8,8,8,8,8,
]
def x(y):
if y == 0:
return 0, 0, 0
elif y == 2:
return 255, 0, 0
elif y == 3:
return 255, 128, 0
elif y == 9:
return 255, 255, 0
elif y == 1:
return 128, 255, 0
elif y == 4:
return 0, 128, 0
elif y == 7:
return 0, 128, 255
elif y == 5:
return 0, 0, 255
elif y == 6:
return 128, 0, 255
elif y == 8:
return 255, 0, 255
elif y == 10:
return 255, 255, 255
import numpy as np
from PIL import Image
b = np.array(list(map(x, a)))
c = b.astype(np.uint8).reshape((72, 8, 3))
Image.fromarray(c).save("numbers.png")
Here as most rated answer (Implementing CRC8 on Arduino to write to MLX90614) is a good example of CRC-8 calculation/finding using a lookup table. I would like to know what is the polynomial used to generate those table values.
So given the table, how can I recover the polynomial? I tried using this nice calculator of tables, but cannot get the same values.
static const uint8_t crc_table[] = {
0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31,
0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d, 0xe0, 0xe7, 0xee, 0xe9,
0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1,
0xb4, 0xb3, 0xba, 0xbd, 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea, 0xb7, 0xb0, 0xb9, 0xbe,
0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16,
0x03, 0x04, 0x0d, 0x0a, 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a, 0x89, 0x8e, 0x87, 0x80,
0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8,
0xdd, 0xda, 0xd3, 0xd4, 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44, 0x19, 0x1e, 0x17, 0x10,
0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f,
0x6a, 0x6d, 0x64, 0x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7,
0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef,
0xfa, 0xfd, 0xf4, 0xf3
};
uint8_t
crc8(uint8_t *p, uint8_t len)
{
uint16_t i;
uint16_t crc = 0x0;
while (len--) {
i = (crc ^ *p++) & 0xFF;
crc = (crc_table[i] ^ (crc << 8)) & 0xFF;
}
return crc & 0xFF;
}
Well, the link you provided shows the exact same table for the (default) polynomial 0x07, which is (x^8) + x^2 + x + 1. So this should already answer your question.
With the above being said, for a general "target" table, you can very easily brute force the needed polynomial.
I'll let the code speak for itself, here's a working example:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
static const uint8_t target[256] = {
// ... fill in the values here ...
};
void compute_table(uint8_t table[256], uint8_t polynomial) {
uint8_t crc = 0x80;
size_t i, j;
memset(table, 0, 256);
for (i = 1; i < 256; i <<= 1) {
if (crc & 0x80)
crc = (crc << 1) ^ polynomial;
else
crc <<= 1;
for (j = 0; j < i; j++)
table[i + j] = crc ^ table[j];
}
}
int main(void) {
uint8_t table[256];
uint8_t poly;
bool found;
size_t i;
for (poly = 1; poly != 0; poly++) {
compute_table(table, poly);
found = true;
for (i = 0; i < 256; i++) {
if (table[i] != target[i]) {
found = false;
break;
}
}
if (found)
printf("Found polynomial: 0x%02x\n", poly);
}
return 0;
}
And indeed, plugging in the table you provide, the above code outputs:
Found polynomial: 0x07
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
unsigned char table[256] = {0};
void crc_table_create(unsigned char *pdata, unsigned char factor)
{
unsigned char temp;
for (int i = 0; i < 256; i++)
{
pdata[i] = i;
}
for (int i = 0; i < 256; i++)
{
for (int j = 7; j >= 0; j--)
{
temp = pdata[i] & 0x80; // take the MSB bit
if (temp) // the MSB bit is 1
{
pdata[i] = pdata[i] << 1;
pdata[i] ^= factor;
}
else
{
pdata[i] = pdata[i] << 1;
}
}
// printf("table[%i]= 0x%02x", i, table[i]);
// getchar();
}
}
int main(void)
{
int i, j = 0;
uint8_t poly = 0x07; // X8 + X2 + X1 + 1 (100000111B the MSB bit is mute, so it turn to 0x07)
crc_table_create(table, poly);
for (i = 0; i < 256; i++)
{
if (i % 12 == 0) {
printf("\n");
}
printf("0x%02x, ", table[i]);
}
printf("\n");
return 1;
}
Here is the CRC8 table generater, after run it, the 'table[256]' is the result, enjoy!
I'm trying to use simple encryption/decryption (treyfer) for a project of mine, but there's no reference implementation of decryption available. What I've got (based off of the Wikipedia implementation) is:
uint8_t rotl(uint8_t x) {
return (x << 1) | (x >> 7);
}
uint8_t rotr(uint8_t x) {
return (x >> 1) | (x << 7);
}
void encrypt (uint8_t text[8], uint8_t key[8]) {
int r, i;
uint8_t t;
for (r=0; r<NUMROUNDS; r++) {
text[8] = text[0];
for (i=0; i<8; i++) {
text[i+1] = rotl((text[i+1] + sbox[(key[i] + text[i]) % 256]));
}
text[0] = text[8];
}
}
void decrypt (uint8_t text[8], uint8_t key[8]) {
int r, i;
uint8_t t;
for (r=0; r<NUMROUNDS; r++) {
text[8] = text[0];
for (i=8; i>=0; --i) {
text[i+1] = rotr(text[i+1]) - sbox[(key[i] + text[i]) % 256];
}
text[0] = text[8];
}
}
void printhex(uint8_t text[8]) {
for(int i = 0; i < 8; i++) {
printf("%02x", text[i]);
}
}
int main() {
char text[8+1] = "AAAAAAAA";
char key[8+1] = "AAAAAAAA";
puts("plaintext:");
printhex(text);
puts("\nkey:");
printhex(key);
puts("\nencrypted:");
encrypt(text, key);
printhex(text);
puts("\ndecrypted:");
decrypt(text, key);
printhex(text);
}
Where Sbox is the AES S-box, and NUMROUNDS = 128
However, when I run this, I get wildly different results for the plaintext and the decrypted value, suggesting either my encryption or decryption is incorrect, but I can't quite see how. Hoping I haven't missed anything too painful.
The output I get is:
plaintext:
4141414141414141
key:
4141414141414141
encrypted:
658ed27d394127a0
decrypted:
a0460a65d755953f
I assume your implementation is based on this: https://en.wikipedia.org/wiki/Treyfer
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define NUMROUNDS 100
static const uint8_t sbox[256] = {
//0 1 2 3 4 5 6 7 8 9 A B C D E F
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
void treyfer_encrypt(uint8_t text[8], uint8_t const key[8])
{
unsigned i;
uint8_t t = text[0];
for (i = 0; i < 8*NUMROUNDS; i++) {
t += key[i%8];
t = sbox[t] + text[(i+1)%8];
text[(i+1) % 8] = t = (t << 1) | (t >> 7); /* Rotate left 1 bit */
}
}
void encrypt(uint8_t text[8], uint8_t const key[8])
{
unsigned int i = 0;
unsigned int j = 0;
uint8_t t = 0;
t = text[0];
for (j = 0; j < NUMROUNDS; j++)
{
for (i = 0; i < 8; i++)
{
t = t + key[i];
t = sbox[t] + text[(i + 1) % 8];
t = (t << 1) | (t >> 7);
text[(i + 1) % 8] = t;
}
}
}
void decrypt(uint8_t text[8], uint8_t const key[8])
{
int i = 0;
int j = 0;
uint8_t top = 0;
uint8_t bottom = 0;
for (j = 0; j < NUMROUNDS; j++)
{
for (i = 7; i >= 0; i--)
{
top = text[i] + key[i];
top = sbox[top];
bottom = text[(i + 1) % 8];
bottom = (bottom >> 1) | (bottom << 7);
text[(i + 1) % 8] = bottom - top;
}
}
}
void printhex(uint8_t text[8]) {
int i;
for(i = 0; i < 8; i++) {
printf("%02x", text[i]);
}
}
int main() {
uint8_t text[8] = "AAAAAAAA";
uint8_t text2[8] = "AAAAAAAA";
uint8_t key[8] = "AAAAAAAA";
puts("plaintext:");
printhex(text);
puts("\nkey:");
printhex(key);
puts("\n\nref encrypted:");
treyfer_encrypt(text, key);
printhex(text);
puts("\n\nencrypted:");
encrypt(text2, key);
printhex(text2);
puts("\n\ndecrypted:");
decrypt(text2, key);
printhex(text2);
puts("\n");
}
results
plaintext:
4141414141414141
key:
4141414141414141
ref encrypted:
cc3121ccab578d93
encrypted:
cc3121ccab578d93
decrypted:
4141414141414141
I have to change this code of 2D array to 1D array but i am not sure how to do it. Because the values of the 2D arrays are in hexadecimal.
int Sbox[SIZE_A][SIZE_B] = { //two dimensional array
{
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76
}, //* initializers for row indexed by 0
{
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf,
0x9c, 0xa4, 0x72, 0xc0
}, //* initializers for row indexed by 1
{
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1,
0x71, 0xd8, 0x31, 0x15
}, //* initializers for row indexed by 2
{
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75
}, {
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
0x29, 0xe3, 0x2f, 0x84
}, {
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39,
0x4a, 0x4c, 0x58, 0xcf
}, {
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
0x50, 0x3c, 0x9f, 0xa8
}, {
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21,
0x10, 0xff, 0xf3, 0xd2
}, {
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73
}, {
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
0xde, 0x5e, 0x0b, 0xdb
}, {
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62,
0x91, 0x95, 0xe4, 0x79
}, {
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea,
0x65, 0x7a, 0xae, 0x08
}, {
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
0x4b, 0xbd, 0x8b, 0x8a
}, {
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e
}, {
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9,
0xce, 0x55, 0x28, 0xdf
}, {
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
0xb0, 0x54, 0xbb, 0x16
}
};
I want to parse the exact values 0x63, 0x7c, 0x77, etc into the 1D array. So I thought of changing the int to string first, then read out the string as 0x63, 0x7c, 0x77, etc to the 1D array. Any help on how to do it?
The first thing you got to remember is that hexadecimal is just a way of presenting integer values, they are not stored as hexadecimal in the computer.
The second thing is that if you want to have a single array made out of a matrix, then the size of that single array would be (in your case) SIZE_A * SIZE_B entries big.
Then it's just a question of looping over the matrix, and setting the values in the array.
You can use the following code (both row-major and column-major order variants - please see comments in the code):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define A_SIZE 2
#define B_SIZE 3
int main() {
int values_2d[A_SIZE][B_SIZE] = {{0x12, 0x24, 0x46},{0x13,0x35, 0x57}};
int values_1d[A_SIZE*B_SIZE] = {0};
int i,j;
for (i = 0; i < A_SIZE; i++) {
for (j = 0; j < B_SIZE; j++) {
printf("v[%d,%d]=0x%02X\n", i, j, values_2d[i][j]);
}
}
for (i = 0; i < A_SIZE*B_SIZE; i++) {
values_1d[i] = values_2d[i/(B_SIZE)][i%B_SIZE]; // <<-- row-major order
//values_1d[i] = values_2d[i%A_SIZE][i/A_SIZE]; // <<-- column-major order
printf("values_1d[%d] = %d (0x%02X), values_2d[%d][%d] = %d (0x%02x)\n",
i, values_1d[i], values_1d[i],
i/B_SIZE, i%B_SIZE,
values_2d[i/B_SIZE][i%B_SIZE], values_2d[i/B_SIZE][i%B_SIZE]);
}
return 0;
}
Result:
┌─(55:55:55)─(michael#lorry)─(~/tmp/hex)
└─► gcc -o main main.c; ./main
values_1d[0] = 18 (0x12), values_2d[0][0] = 18 (0x12)
values_1d[1] = 36 (0x24), values_2d[0][1] = 36 (0x24)
values_1d[2] = 70 (0x46), values_2d[0][2] = 70 (0x46)
values_1d[3] = 19 (0x13), values_2d[1][0] = 19 (0x13)
values_1d[4] = 53 (0x35), values_2d[1][1] = 53 (0x35)
values_1d[5] = 87 (0x57), values_2d[1][2] = 87 (0x57)
It's is in Row-major order.
These are my codes. Is it ok?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define SIZE_A 16 //width
#define SIZE_B 16 //heightint Sbox[SIZE_A][SIZE_B] = { //two dimensional array
{0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
0xfe, 0xd7, 0xab, 0x76}, //* initializers for row indexed by 0
{0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf,
0x9c, 0xa4, 0x72, 0xc0}, //* initializers for row indexed by 1
{0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1,
0x71, 0xd8, 0x31, 0x15}, //* initializers for row indexed by 2
{0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
0xeb, 0x27, 0xb2, 0x75},
{0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
0x29, 0xe3, 0x2f, 0x84},
{0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39,
0x4a, 0x4c, 0x58, 0xcf},
{0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
0x50, 0x3c, 0x9f, 0xa8},
{0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21,
0x10, 0xff, 0xf3, 0xd2},
{0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d,
0x64, 0x5d, 0x19, 0x73},
{0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
0xde, 0x5e, 0x0b, 0xdb},
{0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62,
0x91, 0x95, 0xe4, 0x79},
{0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea,
0x65, 0x7a, 0xae, 0x08},
{0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
0x4b, 0xbd, 0x8b, 0x8a},
{0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9,
0x86, 0xc1, 0x1d, 0x9e},
{0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9,
0xce, 0x55, 0x28, 0xdf},
{0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
0xb0, 0x54, 0xbb, 0x16}
};
int myarray[SIZE_A*SIZE_B]={0};
void print2darray(int Sbox[SIZE_A][SIZE_B])
{
int i,j;
for (i = 0; i < SIZE_A; i++ )
{
for (j = 0; j < SIZE_B; j++ )
{
printf("SBox[%d][%d] = 0x%02X\n",i,j,Sbox[i][j]);
}
}
for (i = 0; i < SIZE_A; i++ )
{
for (j = 0; j < SIZE_B; j++ )
{
myarray[i*SIZE_A+j]=Sbox[i][j];
int value=myarray[i*SIZE_B+j];
printf("myarray is [%d,%d] = 0x%02X\n",i,j,value);
}
}
return;
}
int main()
{
print2darray(Sbox);
return 0;
}
You can declare a pointer to int and allocate memory and convert as shown...
int i, j, k=0;
int *ptr = malloc(sizeof(*ptr) * SIZE_A * SIZE_B);
for(i = 0; i< SIZE_A; i++)
for(j = 0; j< SIZE_B; j++)
{
ptr[k++] = Sbox[i][j];
}
This should solve your problem, if you want the dimensions changed, switch the outer and inner loop
int array1D[SIZEA * SIZEB];
int index1D = 0;
for(int i = 0; i < SIZEB; ++i)
{
for(int j = 0; i < SIZEA; ++j)
{
array1D[index1D++] = Sbox[j][i];
}
}
Nishith was faster
Regarding: i am not sure how to do it. Because the values of the 2D arrays are in hexadecimal.
int Sbox[SIZE_A][SIZE_B] = {...
int Sbox is your first clue. Good news, No conversion necessary.
The numbers represented within each of these blocks are just integers, shown in hexidecimal format. Therefore, no conversion (i.e. from hex to int) is necessary, as they are already int. All you have to do is pick one of the approaches (described in another answer) to re-arrange the values.