Bitmap fonts loading in c - c

I am Working on some bitmap fonts.
The idea here is that i am given 2 files
input.txt and font .txt.
I have to read a string from input.txt file and transform it using the font.txt and print the corresponding output to another file output.txt.
each character in font.txt is represented by a grid of 16x8. e.g.:
"A"=
0x00,
0x00,
0x10,
0x38,
0x6c,
0xc6,
0xc6,
0xfe,
0xc6,
0xc6,
0xc6,
0xc6,
0x00,
0x00,
0x00
0x00
Can Someone please Just give me an idea how to load the above format stored in a file into a data structure.

Take a look at this SO solution to see how to read line by line in c.
I'm assuming you work with ASCII and your array size is constant. You can simply check for the " at the beginning of each line, in which case you can assume it's an identifier for your letter, else you read the values line by line into the 16x8 array, dropping the , at the end if there is one.
Checking for the " can simply be done by a direct comparison since it's an ascii character.
if (myline[0] == 34) {...};
Getting the letters could be done the same way:
char myletter = myline[1];
You could also use scanf to parse your line formatted. Also if you have to possibility to work in C++, it will make your life a lot easier since you'll have access to higher level methods.

Related

Determine which variable use at runtime in Embedded C

There are two different makefiles for this firmware that I am working on.
Data Version 1 and Data Version 2. Both versions are using the same file called ble_communication.c in each own make file.
To differentiate between the two versions, we have two variables declared inside ble_communication.c
static uint8_t data_version1[] = {0x00, 0x00, 0x00, 0x00, 0x00};
static uint8_t data_version2[] = {0x01, 0x00, 0x00, 0x00, 0x00};
Inside the ble_communication.c file we have a function called
uint32_t start_broadcasting(void)
{
send_beacon(OPCODE, 0, /* determine data version here to send at runtime */, key);
}
My question is since we are using the same file ble_communication.c for both versions of the build, how can the code select which variable to use for its build during the runtime of the code? If it's Data Version 1, I want it to use data_version1[] and if's is Data Version 2 it uses data_version2[].
I can't use #ifndef switch statements as I am not allowed to use them due to the new design guidelines
To be honest, I would prefer to use the #ifdef, but here is a workaround for you.
Create two files with the desired data and select the required file at build time, using the makefile.
First, prepare two C files, ble_communication_data1.c and ble_communication_data2.c. Feel free to choose a clearer name.
Place the required data in each C file, but keep the names the same.
ble_communication_data1.c
uint8_t ble_communication_data[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
ble_communication_data2.c
uint8_t ble_communication_data[5] = {0x01, 0x00, 0x00, 0x00, 0x00};
Create a header file to access the data:
ble_communication_data.h
extern uint8_t ble_communication_data[5];
Modify the ble_communication.c so that it uses the common variable name:
#include "ble_communication_data.h"
uint32_t start_broadcasting(void)
{
send_beacon(OPCODE, 0, ble_communication_data, key);
}
Finally, in each of the makefiles, add the correct ble_communication_data C file to the list of files to be compiled.

How to append another HEX value at the end of an existing byte array in C

I have searched on google and checked my findings on https://www.onlinegdb.com/
But so far, I am not satisfied with my trials and errors. Perhaps, I didn't know how to ask.
I am sure that this could be already very known by many people.
Normally I am reading HEX values from UART communication and placing in a buffer array.
But, for making things simpler, I give you that code snippet;
uint8_t buffer[20] = {0x7E, 0x00, 0x07, 0xAA, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xCC};
uint8_t newValue = 0x55;
My goal is to append newValue on buffer and that new value has to be seen after the last array value which is 0xCC in this case.
So, my question is how to do that efficiently?
Note that: One of my trials (works OK but not as I wanted);
buffer[11] = newValue ;
for(int i=0;i<sizeof(buffer);i++)
printf("%02x", buffer[i]);
But, then I need to know the position of the last value and increase the position by one which is 11 (0 based counting) in this case.

how to create image programmatically

Is it possible to create bitmap programmatically in C?
I want to pass it some text to draw, e.g.
createBitmapWithContents("Hello");
or
createBitmapWithContents("Hello \n other line");
And it should create bitmap which has "Hello" drawn in it (or draw second text respectively).
Also, the text "Hello" might be a Unicode string. Not necessarily English characters.
Preferably I would like to do this without using some third party libraries.
You'll need to do two different things :
Generate an image in memory that represents your string
Store that image into a file
Both can be done without external libraries (using simple predefined patterns of characters and storing as simple format such as BMP).
But note that it would be a lot easier to do this using a high-level image drawing library such as OpenCV or ImageMagick.
The first thing to do is to is to define a data structure to store your image, something like this:
struct Image {
int height, width;
unsigned char* pixels;
};
Then you'll have to generate the functions to allocate the image, free the image and maybe something to copy an image inside another one.
In order to print your character on your image, you would have to create predefined patterns like this:
char patternA[] = {
0x00, 0x00, 0xff, 0x00, 0x00
0x00, 0xff, 0x00, 0xff, 0x00
0x00, 0xff, 0x00, 0xff, 0x00
0x00, 0xff, 0xff, 0xff, 0x00
0x00, 0xff, 0x00, 0xff, 0x00
0x00, 0xff, 0x00, 0xff, 0x00
0x00, 0xff, 0x00, 0xff, 0x00
};
Image imageOfA;
imageOfA.width = 5;
imageOfA.height= 7;
imageOfA.pixels= patternA;
You can also read those patterns from image files or even better, from a font file (but without external libraries, you'll need to implement the file readers yourself).
Once you have your patterns of characters, you can combine those predefined images to create a new image corresponding to your input string.
Finally, you'll have to write your image structure into a file. For that, you can either use a low-level library (such as libjpeg or libpng) or you can implement it yourself using a simple file format (such as BMP).
The conclusion is that you really want to use a third party library to achieve what you want.
Did you try googling this?
There's quite a few things you could do, for example you can you can run loops to create your own matrix of pixels. check this link here

Use NSTextfield data to build an array of integers?

Im using the following code to send a hex string to an NSStreamoutput:
uint8_t mirandaroutecommand[] = { 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x30, 0x14 };
NSData *data = [[NSData alloc] initWithBytes:mirandaroutecommand length:sizeof(mirandaroutecommand)];
[hextooloutputStream write:[data bytes] maxLength:[data length]];
This works great but the problem is I need these hex values to come from NSTextfields on the user interface. I've tried to convert the NSTextfield data to an integer but that didn't work. Is there a way to use data from several NSTextfields to build an array of integers using hex values?
I've tried to find an existing topic covering this but I've had no luck.
The NSString methods integerValue and intValue assume a decimal representation and ignore and trailing gumph - so apply them to #"0x01" and they see 0 followed by some gumph (x01) which they ignore.
To read a hex value use NSScanner's scanHexInt: (or scanHexLongLong:):
unsigned scannedHexValue;
if ([[NSScanner scannerWithString:textFieldContents] scanHexInt:&scannedHexValue])
{
// value found
...
}
NSScanner is more powerful than just parsing a single value, so with the appropriate method calls you can scan a comma separated list of hex values if you wish.

AES Plaintext to Ciphertext Encryption

I am currently working through Practical Cryptography and am trying to do the exercises at the end of chapter 4. There are a couple of of questions that ask you to decrypt a bit of ciphertext in hex with a key also in hex. I am using the openssl crypto libraries to try and accomplish this.
I am having trouble knowing whether I have done this correctly as I have done it a number of ways and I get different answers. Also because the values provided are in hex I am having a bit of a hard time getting them into a usable value.
I am just using printf to put the hex values into a string. This seems correct to me as I can read them out again and they are correct. The key is harder, I am trying to store the numbers directly into the key structure provided by openssl but I dont know how the openssl implementation uses the key structure.
My code is below. If I run it in this format I get the following output.
In : 0x53:0x9b:0x33:0x3b:0x39:0x70:0x6d:0x14:0x90:0x28:0xcf:0xe1:0xd9:0xd4:0xa4:0x7
Out: 0xea:0xb4:0x1b:0xfe:0x47:0x4c:0xb3:0x2e:0xa8:0xe7:0x31:0xf6:0xdb:0x98:0x4e:0xe2
My questions are:
Does my method of storing the key look correct?
Does my overall method look correct?
Does anyone know what the actual answer should be?
Code below
int main( void )
{
unsigned char InStr [100];
unsigned char OutStr[100];
char KeyStr[100];
AES_KEY Key;
Key.rd_key[0] = 0x8000000000000000;
Key.rd_key[1] = 0x0000000000000000;
Key.rd_key[2] = 0x0000000000000000;
Key.rd_key[3] = 0x0000000000000001;
Key.rounds = 14;
sprintf( InStr, "%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\0",
0x53, 0x9B, 0x33, 0x3B, 0x39, 0x70, 0x6D, 0x14,
0x90, 0x28, 0xCF, 0xE1, 0xD9, 0xD4, 0xA4, 0x07 );
AES_decrypt( InStr, OutStr, &Key );
printf( "In : %#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx\n",
InStr[0], InStr[1], InStr[2], InStr[3], InStr[4], InStr[5], InStr[6], InStr[7],
InStr[8], InStr[9], InStr[10], InStr[11], InStr[12], InStr[13], InStr[14], InStr[15] );
printf( "Out: %#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx:%#hx\n",
OutStr[0], OutStr[1], OutStr[2], OutStr[3], OutStr[4], OutStr[5], OutStr[6], OutStr[7],
OutStr[8], OutStr[9], OutStr[10], OutStr[11], OutStr[12], OutStr[13], OutStr[14], OutStr[15] );
return 0;
}

Resources