gfx2gba doesn't convert image properly - c

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,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 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")

Related

aes 128 encryption and decryption in c without any seperate libraries and header files needed mainly in diagnostics point of view

#include <stdio.h>
#include <stdint.h>
#define Nb 4
#define Nk 4
#define Nr 10
uint8_t state[4 * Nb];
uint8_t w[Nb * (Nr + 1) * 4];
#define mul2(x) ((x << 1) ^ (((x >> 7) & 1) * 0x1b))
#define mul3(x) (mul2(x) ^ x)
#define mul9(x) (mul2(mul2(mul2(x))) ^ x)
#define mul11(x) (mul2(mul2(mul2(x)) ^ x) ^ x)
#define mul13(x) (mul2(mul2(mul2(x) ^ x)) ^ x)
#define mul14(x) (mul2(mul2(mul2(x) ^ x) ^ x))
i dont want to use lookup table for xor multiplications so used this above method as reference to caliculate the values runtime
uint8_t sbox[256] = {
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, 0x3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0x80, 0x1b,
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d
};
uint32_t sub_bytes_word(uint32_t word) {
uint8_t* bytes = (uint8_t*)&word;
for (int i = 0; i < 4; i++) {
bytes[i] = sbox[bytes[i]];
}
return word;
}
uint32_t inv_sub_bytes_word(uint32_t word) {
uint8_t* bytes = (uint8_t*)&word;
for (int i = 0; i < 4; i++) {
bytes[i] = inv_sbox[bytes[i]];
}
return word;
}
uint32_t rot_word(uint32_t word) {
return (word << 8) | (word >> 24);
}
uint32_t sub_word(uint32_t word) {
return sub_bytes_word(word);
}
uint32_t inv_word(uint32_t word) {
return inv_sub_bytes_word(word);
}
void key_expansion(uint8_t* key) {
for (int i = 0; i < Nk; i++) {
w[i] = key[4 * i] << 24 | key[4 * i + 1] << 16 | key[4 * i + 2] << 8 | key[4 * i + 3];
}
for (int i = Nk; i < 4 * (Nr + 1); i++) {
uint32_t temp = w[i - 1];
if (i % Nk == 0) {
temp = sub_word(rot_word(temp)) ^ rcon[i / Nk - 1];
} else if (Nk > 6 && i % Nk == 4) {
temp = sub_word(temp);
}
w[i] = w[i - Nk] ^ temp;
}
for (int i = 0; i < 4 * (Nr + 1); i++) {
w[i] = inv_word(w[i]);
}
}
void sub_bytes() {
for (int i = 0; i < 4 * Nb; i++) {
state[i] = sbox[state[i]];
}
}
void shift_rows() {
uint8_t temp[4 * Nb];
for (int i = 0; i < 4 * Nb; i++) {
temp[i] = state[i];
}
for (int i = 0; i < Nb; i++) {
state[i] = temp[i];
state[i + 4] = temp[i + 4];
state[i + 8] = temp[i + 8];
state[i + 12] = temp[i + 12];
}
state[1] = temp[5];
state[5] = temp[9];
state[9] = temp[13];
state[13] = temp[1];
state[2] = temp[10];
state[6] = temp[14];
state[10] = temp[2];
state[14] = temp[6];
state[3] = temp[15];
state[7] = temp[3];
state[11] = temp[7];
state[15] = temp[11];
}
void mix_columns() {
uint8_t temp[4 * Nb];
for (int i = 0; i < 4 * Nb; i++) {
temp[i] = state[i];
}
for (int i = 0; i < Nb; i++) {
state[0 + 4 * i] = mul2(temp[0 + 4 * i]) ^ mul3(temp[1 + 4 * i]) ^ temp[2 + 4 * i] ^ temp[3 + 4 * i];
state[1 + 4 * i] = temp[0 + 4 * i] ^ mul2(temp[1 + 4 * i]) ^ mul3(temp[2 + 4 * i]) ^ temp[3 + 4 * i];
state[2 + 4 * i] = temp[0 + 4 * i] ^ temp[1 + 4 * i] ^ mul2(temp[2 + 4 * i]) ^ mul3(temp[3 + 4 * i]);
state[3 + 4 * i] = mul3(temp[0 + 4 * i]) ^ temp[1 + 4 * i] ^ temp[2 + 4 * i] ^ mul2(temp[3 + 4 * i]);
}
}
void add_round_key(int round) {
for (int i = 0; i < 4 * Nb; i++) {
state[i] ^= w[round * 4 * Nb + i];
}
}
void aes_round(int round) {
sub_bytes();
shift_rows();
mix_columns();
add_round_key(round);
}
void inv_sub_bytes() {
for (int i = 0; i < 4 * Nb; i++) {
state[i] = inv_sbox[state[i]];
}
}
void inv_shift_rows() {
uint8_t temp[4 * Nb];
for (int i = 0; i < 4 * Nb; i++) {
temp[i] = state[i];
}
for (int i = 0; i < Nb; i++) {
state[i] = temp[i];
state[i + 4] = temp[i + 4];
state[i + 8] = temp[i + 8];
state[i + 12] = temp[i + 12];
}
state[1] = temp[13];
state[5] = temp[1];
state[9] = temp[5];
state[13] = temp[9];
state[2] = temp[10];
state[6] = temp[14];
state[10] = temp[2];
state[14] = temp[6];
state[3] = temp[7];
state[7] = temp[11];
state[11] = temp[15];
state[15] = temp[3];
}
void inv_mix_columns() {
uint8_t temp[4 * Nb];
for (int i = 0; i < 4 * Nb; i++) {
temp[i] = state[i];
}
for (int i = 0; i < Nb; i++) {
state[0 + 4 * i] = mul14(temp[0 + 4 * i]) ^ mul11(temp[1 + 4 * i]) ^ mul13(temp[2 + 4 * i]) ^ mul9(temp[3 + 4 * i]);
state[1 + 4 * i] = mul9(temp[0 + 4 * i]) ^ mul14(temp[1 + 4 * i]) ^ mul11(temp[2 + 4 * i]) ^ mul13(temp[3 + 4 * i]);
state[2 + 4 * i] = mul13(temp[0 + 4 * i]) ^ mul9(temp[1 + 4 * i]) ^ mul14(temp[2 + 4 * i]) ^ mul11(temp[3 + 4 * i]);
state[3 + 4 * i] = mul11(temp[0 + 4 * i]) ^ mul13(temp[1 + 4 * i]) ^ mul9(temp[2 + 4 * i]) ^ mul14(temp[3 + 4 * i]);
}
}
void inv_aes_round(int round) {
add_round_key(round);
inv_mix_columns();
inv_shift_rows();
inv_sub_bytes();
}
Here i tried encrypt and decrypt the plain text
void encrypt(uint8_t* plaintext, uint8_t* key) {
for (int i = 0; i < 4 * Nb; i++) {
state[i] = plaintext[i];
}
key_expansion(key);
add_round_key(0);
for (int i = 1; i <= Nr; i++) {
aes_round(i);
}
}
void decrypt(uint8_t* ciphertext, uint8_t* key) {
for (int i = 0; i < 4 * Nb; i++) {
state[i] = ciphertext[i];
}
key_expansion(key);
add_round_key(Nr);
for (int i = Nr - 1; i >= 1; i--) {
inv_aes_round(i);
}
inv_aes_round(0);
}
My main function
int main() {
// Set key and plaintext
uint8_t key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
uint8_t plaintext[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
uint8_t ciphertext[16];
uint8_t decrypted[16];
// Plaintext
printf("Original plaintext: ");
for (int i = 0; i < 16; i++) {
printf("%02x ", plaintext[i]);
}
printf("\n");
// Encrypt plaintext
encrypt(plaintext, key);
printf("Ciphertext: ");
for (int i = 0; i < 4 * Nb; i++) {
printf("%02x ", state[i]);
ciphertext[i] = state[i];
}
printf("\n");
// Decrypt ciphertext
decrypt(ciphertext, key);
printf("Decrypted: ");
for (int i = 0; i < 4 * Nb; i++) {
printf("%02x ", state[i]);
decrypted[i] = state[i];
}
printf("\n");
return 0;
}
i have tried improving the code many times by assigning seperate functions to implement but every time my output is wrong .\
This is my output ; whatever i tried iam not able to get correct decrypt . iam not sure where i did mistake .
Original plaintext: 01 23 45 67 89 ab cd ef 01 23 45 67 89 ab cd ef
Ciphertext: ec d5 9c a4 41 cb cb e5 08 02 a3 cc 7c 39 be 74
Decrypted: 37 b8 75 c5 01 5c 74 14 b4 e9 11 d8 3c 16 cd c2

How do I get text to show on screen?

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;
}

C Segmentation fault, when the program is run in the terminal, but runs and returns 0 in Eclipse

I'm working on a program that prints 2 strings on a ppm image file.
My program seems to work, when run in eclipse and i can see my image being updated, but when I run it in C, I get Segmentation fault(core dumped).
main.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "font8x8_basic.h"
#include "func.h"
int main(){
ppm *h = store("image.ppm");
printf("\n Header: %s %d, %d, %d \n Pixels: %d\n", h->type, h->width, h->height, h->color_max, *h->pixel_pointer);
printchar(h, ' ', 0);
char *str="Naruto does brrrr";
for (int i=0; i<strlen(str); i++){
ppm *s = store("out.ppm");
printchar(s, *(str+i), (i)*10);
}
for (int i=0; i<strlen(str); i++){
ppm *s = store("out.ppm");
printchar(s, *(str+i), ((i)*10+(s->width)*(s->height-10)));
}
return 0;
}
func.h:
#ifndef FUNC_H_
#define FUNC_H_
typedef struct ppm{
//P3 = ASCII; P6 = BINARY
char *type;
//Will be used as matrix dimentions
unsigned int width;
unsigned int height;
unsigned int color_max;
unsigned int *pixel_pointer;
unsigned int num_el;
}ppm;
void printchar(ppm *img, unsigned char ch, int offset){
FILE *out = fopen("out.ppm", "w");
fprintf(out,"%s \n%d %d\n%d\n ", img->type, img->width, img->height, img->color_max);
int set;
int x=0;
int new_row=0;
for(int c=1; c<((img->num_el/3)+x); c++){
new_row = x*((img->width))+(img->width)/4+2+offset;
if((x<8)&&((c==new_row+x))){
for (int y=0; y < 8; y++) {
set = font8x8_basic[ch][x] & 1 << (y);
if(set){
fprintf(out, "%d %d %d ",0,0,0);
img->pixel_pointer = img->pixel_pointer+3;
}
else{
fprintf(out, "%d %d %d ",*img->pixel_pointer,*(img->pixel_pointer+1),*(img->pixel_pointer+2));
img->pixel_pointer = img->pixel_pointer+3;
}
c++;
}
x++;
}
else{
fprintf(out, "%d %d %d ",*img->pixel_pointer,*(img->pixel_pointer+1),*(img->pixel_pointer+2));
img->pixel_pointer = img->pixel_pointer+3;
}
}
fclose(out);
}
unsigned int *endptr;
ppm *store(char *filename){
ppm *input_file = malloc(sizeof(ppm));
input_file->type = malloc(sizeof(char)*3);
FILE *fp= fopen(filename, "rb");
fscanf(fp, "%s\n", input_file->type);
fscanf(fp, "%d %d\n", &input_file->width, &input_file->height);
fscanf(fp, "%d\n", &input_file->color_max);
int i=0;
input_file->pixel_pointer = malloc((input_file->width)*(input_file->height)*sizeof(unsigned int)*3);
for(int c=0; c<(3*(input_file->width)*(input_file->height)); c++){
fscanf(fp, "%d ", (input_file->pixel_pointer+i));
i++;
}
input_file->num_el = i;
fclose(fp);
return input_file;
}
GDB and Valgrind point to my second call of the store() function for the segmentation fault.
Any help would be appreciated!
edit: font8x8_basic.h:
char font8x8_basic[128][8] = {
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0000 (nul)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0001
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0002
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0003
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0004
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0005
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0006
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0007
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0008
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0009
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000F
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0010
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0011
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0012
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0013
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0014
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0015
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0016
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0017
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0018
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0019
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001F
{ 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
};

Finding polynomial used in CRC-8 calculation table

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,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x2c, 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!

how to convert 2D to 1D array in C when int has hexadecimal

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.

Resources