I am looking for a simple way to convert from c-code hex array to matlab
Available C-Code Format:
const uint16_t AUDIO_SAMPLE[] = {0x4952, 0x4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1}
Required Matlab Format:
AUDIO_SAMPLE = [18770 17990 20334 15 16727 17750 28006 8308 18 0 1]
Though it is simple to convert for small numbers, I have to convert a very big array. I am using embedded platform so the numbers can not be written to a simple text file for later read in matlab. Its preferable to write the conversion code in matlab.
Edit:
Till now able to get rid of 0x. Failed to get a vector after using eval function as given below:
a='0x4952, 0x4646, 0x4f6e'; %Given
b = strrep(a, '0x', '') ; %Returns 4952, 4646, 4f6e
x = eval( [ '[', b, ']' ] ) %
Copy the code that defines the array to a file called "clip.h".
Write a small c program that #include "clip.h" and use a simple loop to write the data in the desired format. Run the program on your desktop computer. It could look something like this:
#include <stdio.h>
#include <stdint.h>
#include "clip.h"
#define NUMBERS_PER_LINE 20
int main(void) {
int i;
int sample_length = (sizeof AUDIO_SAMPLE)/(sizeof AUDIO_SAMPLE[0]);
printf("AUDIO_SAMPLE = [ ");
for (i=0; i < sample_length; i++) {
printf("%" PRIu16 PRId16, AUDIO_SAMPLE[i]); // Use the format specifier for uint16_t.
if (i % NUMBERS_PER_LINE == NUMBERS_PER_LINE - 1) {
// Insert line continuation mark
printf(" ...\n ");
}
}
return 0;
}
The program will write the matlab code on stdout, so you need to redirect it to the desired file.
It proved tougher than I thought.
Contents of the text file audio.c
4952, 4646, 0x4f6e, 0xf, 0x4157, 0x4556, 0x6d66, 0x2074, 0x12, 0, 0x1,
0x2, 0xbb80, 0, 0xee00, 0x2, 0x4, 0x10, 0, 0x6166, 0x7463, 0x4,
0, 0xd3cf, 0x3, 0x6164, 0x6174, 0x4f3c, 0xf, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
The given file has 11 values in each line. Here follows the MATLAB code to convert HEX values to decimal numbers:
fp = fopen('audio.c', 'rt');
C = textscan(fp, '%s', 'CommentStyle', '#', 'Delimiter', '');
fclose(fp);
C = regexp(C{:}, '\w+', 'match');
C = cellfun(#(x)strrep(x,'0x',''), C, 'UniformOutput', false);
C = cellfun(#(x)hex2dec(x), C, 'UniformOutput', false);
result=cell2mat(C)'
allOneString = sprintf('%d, ' , result)
Finally I have string of decimal values separated by comma as follows:
18770, 17990, 20334, 15, 16727, 17750, 28006, 8308, 18, 0, 1, 2, 48000, 0, 60928, 2, 4, 16, 0, 24934, 29795, 4, 0, 54223, 3, 24932, 24948, 20284, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Of course previous posts on SO were of immense help :)
Related
I'm trying to translate a sentence into morse code. I have a morse.txt file with this type of content :
'-','-','-','-','-',0, /* Caractère 0 */
'.','-','-','-','-',0, /* Caractère 1 */
'.','.','-','-','-',0, /* Caractère 2 */
In order to translate each letter from the sentence I thought about create a 2 dimension array in which we would find a letter and its translation in morse. Here's my struct
typedef struct
{
char lettre;
char *morse;
} t_morse;
I created a function returning that array :
t_morse *loadTab()
{
t_morse *tabMorse = malloc(LETTER_NUMBER * sizeof(t_morse));
ssize_t read;
size_t len = 0;
char index = 0, k = 0, j;
char *line = NULL;
FILE *f = fopen("morse.txt", "r");
if (f == NULL)
{
printf("Impossible d'ouvrir morse.txt\n");
exit(0);
}
while (read = getline(&line, &len, f) != -1 && line != NULL)
{
char *morse = malloc(6 * sizeof(char));
char *stringfinal = malloc(50 * sizeof(char));
strcpy(stringfinal, line);
k = 0;
for (j = 0; j < strlen(stringfinal); j++)
if (stringfinal[j] == '-' || stringfinal[j] == '.')
morse[k++] = stringfinal[j];
tabMorse[index].morse = morse;
tabMorse[index].lettre = index + 48;
printf("lettre : %c, morse : %s\n", tabMorse[index].lettre, tabMorse[index].morse);
index++;
free(morse);
free(stringfinal);
}
fclose(f);
for (index = 0; index < LETTER_NUMBER; index++)
{
printf("toto %c : %s\n", tabMorse[index].lettre, tabMorse[index].morse);
}
return tabMorse;
}
But it doesn't as it would be : when executing the programm, the first printf (printf("lettre : %c, morse : %s\n", tabMorse[index].lettre, tabMorse[index].morse);) show me the thing that I want. However after this when I want to iterate through that array and display the structs, the letter field is the right but in the morse field I get a "" string, and I don't know why.
You can see below, a screen of the stdout when launching the program :
Do you know why it acts like that ?
The immediate problem is that you call free(morse); around every iteration of the loop. So when you print it from the second loop the contents are undefined.
You also do not null terminate the morse[] string.
tabMorse[index].morse = morse;
...
free(morse);
Since tabMorse[index].morse is equal to morse, free(morse) is the same as free(tabMorse[index].morse), which is obviously not right.
You do not want to free the chunk of memory that you just stashed a pointer to. You need to keep it allocated so you can safely dereference the pointer later.
As others have noted (no use repeating), you cannot use pointers to heap memory that has been free()'d. (It's best to not duplicate pointers unless you really keep track of them.)
tabMorse[index].morse = morse;
/* 3 lines omitted */
free(morse);
Too much code hides problems such as this.
Take a step back and think about Morse Code. There are only two symbols, and no character is longer than 5 symbols (or is it 6 maximum?).
A byte (unsigned) has 8 bits that can be in one of two states.
Imagine "encoding" the Morse alphabet in the lowest bits of a byte, one byte per character. Because Morse code uses 1 to 5 symbols per character, another bit (on the left of the low bits) can be used as a flag meaning "the remaining bits on my right are to be used as 0=dot and 1=dash"... The Morse letter 'E' (one dot only) would be represented by 0b000000t0, where 't' would be the 'trigger' (always 1), and the lowest bit 0 would signify a single 'dot'.
The familiar "SOS" would be 0b00001000, 0b00001111, 0b00001000 ("... --- ...").
The code below has been adapted from something I wrote for Brail translation. The (incomplete) octal values in the table have NOT been checked as valid Brail but give you a start as to how you might, with the description above, encode 128 characters (7-bit ASCII) in Morse (replacing the Brail encoding). (The tables entries for 'S/s', 'O/o' and 'SP' have been adapted to be correct Morse characters.)
It seems this is a lot less code, and doesn't provide many corners for bugs to lurk.
void morsify( char c ) {
unsigned char oct[] = { // TODO: adapt these values from Brail to Morse
// CTRL codes
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// CTRL codes
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// punctuation
000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 002, 0, 0, 0,
// punctuation & digits
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 0, 0, 0, 0, 0,
// # + A-O
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 005, 007, 015, 035, 017,
// P-Z + braces
017, 037, 027, 010, 036, 045, 047, 072, 055, 075, 065, 0, 0, 0, 0, 0,
// ` + a-o
0, 001, 003, 011, 031, 021, 013, 033, 023, 012, 032, 005, 007, 015, 035, 017,
// p-z + braces
017, 037, 027, 010, 036, 045, 047, 072, 055, 075, 065, 0, 0, 0, 0, 0,
};
bool trig = false;
uint8_t morse = oct[ c ]; // fetch copy of bit pattern from table
for( uint8_t i = 0x80; i; i >>= 1 )
if( !trig && (i & morse) ) // keep scanning right until trigger bit encountered
trig = true;
else if( trig ) // output has been triggered
putchar( i & morse ? '-' : '.' ); // 1's = '-', 0's = '.'
putchar( ' ' ); // space between characters
}
int main() {
char str[] = "Stuff happens";
puts( str );
for( int i = 0; str[ i ]; i++ )
morsify( str[ i ] );
putchar( '\n' );
char str1[] = "SOS sos SOS";
puts( str1 );
for( i = 0; str1[ i ]; i++ )
morsify( str1[ i ] );
putchar( '\n' );
return (0);
}
Stuff happens
... ---. ..-.- .-- .-- ..-- --- --- ...- --.- ...
SOS sos SOS
... --- ... ... --- ... ... --- ...
If you want to "buffer-up" the characters, simply replace the putchar() with your buffering scheme. Then you could output one long string instead of each individual dot/dash & SP.
I want do to a loop with some variables on my arduino , I have 24 variables ( ConfigSonde[0][3] to ConfigSonde[24][3] ) to change, i need do to a loop:
EX :
ConfigSonde_0[] = {'xxx', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
ConfigSonde_1[] = {'xxx', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
for (i = 0; i < 25; i = i + 1) { ConfigSonde_[i][3]=0;}
Is it possible ?
Thank
You are mixing ints and 'xxx'? (if it's supposed to be a string it must be "xxx"), and this is not legal in C.
ConfigSonde_[i][3] where i = 0 doesn't give you a reference to the variable ConfigSonde_0[3]
Maybe you are looking for:
int ConfigSonde_0[] = {'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0};
int ConfigSonde_1[] = {'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0};
int *ConfigSonde [] = {ConfigSonde_0, ConfigSonde_1};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
or
int ConfigSonde[][10] = {
{'x', 3, 'A', 1, 0, 0, 0, 0, 0, 0},
{'x', 1, 'A', 1, 0, 0, 0, 0, 0, 0}
};
for (i = 0; i < 2; i = i + 1) {ConfigSonde[i][3] = 0;}
Rewrite: Do do what you need, you're going to have to either add additional variables that point at the things you want to change in the loop, or change your 25 distinct arrays to be a single 2-dimensional array.
You cannot dynamically "compute" names of variables to access their values in C (or C++), since these languages are compiled the names of variables are no longer relevant when the program runs.
I am writing a C program to generate Keys and test them in an encryption function. However since I have NEVER written a C program before, and I'm completely unused to having manage memory manually, I have run into a problem. I have a memory leak and to be honest, I don't know how to solve it. I understand I need to release the memory at some point but can't until I've run through all keys and I run out of memory before I get through all of them. Writing the program in a different language is NOT an option so please do not suggest that. The code that is leaking is shown below, any help would be appreciated.
EDIT: I know i haven't called a free function to free the memory. I don't see where I can put it because I need the memory until i run through all keys. Putting it outside the loops doesn't solve the problem because the leak occurs inside the loops
2nd EDIT: Posted the full program. I do not have the option of using data structures (i.e. the bool arrays) other than those shown because of how the DES encrypt function(which I did not write) works
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
void dec2bin(bool *testaRR, bool *to_return, int convert);
int main(int argc, const char * argv[])
{
// insert code here...
bool testKey[56] = {
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1
};
bool testKey2[56] = {//intuitive key reversed for testing
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0
};
bool output[64];
bool input[64] = {//the reverse of below... DES bits are numbered left to right, in order of least to most significant so we must enter the bit values in reverse.
//forexample the binary vale of N is 01001110 but below is displayed as 01110010
1, 0, 0, 0, 1, 1, 0, 0,//1
0, 0, 0, 0, 1, 1, 0, 0,//0
1, 1, 0, 0, 0, 0, 1, 0,//C
1, 0, 1, 0, 0, 0, 1, 0,//E
1, 1, 0, 0, 1, 0, 1, 0,//S
0, 0, 1, 0, 1, 0, 1, 0,//T
1, 0, 1, 0, 0, 0, 1, 0,//E
0, 1, 1, 1, 0, 0, 1, 0 //N
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int i = y-4; i >= 0; i-=4)
printf("%X", input[i]+2*input[i+1]+4*input[i+2]+8*input[i+3]);//this is the conversion to hex code
printf(")\n");
/*
use char[] to store the key as set of
*/
/*bool input[64] = {//this is the given plaintext message in the intuitive order (opposite of what it is)
0, 1, 0, 0, 1, 1, 1, 0,//N
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 1, 0, 1, 0, 0,//T
0, 1, 0, 1, 0, 0, 1, 1,//S
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 0, 0, 0, 1, 1,//C
0, 0, 1, 1, 0, 0, 0, 0,//0
0, 0, 1, 1, 0, 0, 0, 1 //1
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int j = 0; j < y; j+=4)
printf("%X", input[j+3]+2*input[j+2]+4*input[j+1]+8*input[j]);//this is the conversion to hex code
printf(")\n");*/
bool test [8];
bool returned[8];
char keyphrase [8];
keyphrase[7] = 0;
for(int start = 65; start<=90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for(int two = 65; two<=90; two++){
keyphrase[1]=two;
for(int three = 65; three<=90; three++){
keyphrase[2]=three;
for(int four = 65; four<=90; four++){
keyphrase[3]=four;
for(int five = 65;five<=90;five++){
keyphrase[4]=five;
for( int six = 65; six <=90; six++){
keyphrase[5]=six;
for(int seven = 65; seven <=90; seven++){
keyphrase[6]=seven;
printf("%s \n", keyphrase);
}
}}
}
}
}
//once i fix the memory leak I will be calling the EncryptDes Function here and checking the outputblk agains the given cipher text
}
free(keyphrase);
int k = sizeof(testKey);
printf("(Test Key: ");
for (int z = 0; z < k; z+=7)
printf("%d", testKey[z+7]+2*testKey[z+6]+4*testKey[z+5]+8*testKey[z+4]+16*testKey[z+3]+32*testKey[z+2]+64*testKey[z+1]+ 128*testKey[z]);//this is the conversion to hex code
printf(")\n");
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
printf("(Output MSG: ");
for (int i = 0; i < x; i+=4)
printf("%X", output[i+3]+2*output[i+2]+4*output[i+1]+8*output[i]);//this is the conversion to hex code
printf(")\n");
return 0;
}
void dec2bin (bool *testaRR, bool *to_return, int convert)
{
printf("%d : ", convert);
printf("%c", convert);
printf("\n ");
//bool testaRR [8];
for(int st = 0; st<8; st++){
testaRR[st] = convert%2;
to_return[7-st] = testaRR[st];
//printf("%d :", 7-st);
//printf(" %d spot ", st);
convert = convert/2;
//testaRR stores the arrays in one direction
//to_return stores them in the other
//Example:
//65 = 01000001 testaRR least significant on the far right (m0st sig is in index 7)better for storage and keeping track of where the bits actually are in binary
//65 = 10000010 to_return least significant on the far left (same as DES) (most significant bit is index 0) good for printing to screen
}
You do not need dynamic memory management here.
Start with
char keyphrase[8];
keyphrase[7]=0;
instead of your malloc and you will be good to go. Your highest array index is 7 (the terminating NUL), and hence you need an array of 8 items (0..7).
If you really want to use malloc, simply a free() at the end will be fine, but you need to malloc 8 characters and set keyphrase[7] to 0 to do the terminating NUL still.
Here's a tested version that works:
#include <stdio.h>
/* compile with gcc -Wall -std=c99 keyphrase.c -o keyphrase */
int
main (int argc, char **argv)
{
char keyphrase[8];
keyphrase[7] = 0;
for (int start = 65; start <= 90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for (int two = 65; two <= 90; two++)
{
keyphrase[1] = two;
for (int three = 65; three <= 90; three++)
{
keyphrase[2] = three;
for (int four = 65; four <= 90; four++)
{
keyphrase[3] = four;
for (int five = 65; five <= 90; five++)
{
keyphrase[4] = five;
for (int six = 65; six <= 90; six++)
{
keyphrase[5] = six;
for (int seven = 65; seven <= 90; seven++)
{
keyphrase[6] = seven;
printf ("%s \n", keyphrase);
}
}
}
}
}
}
}
}
The real problem is the use of printf. You did not NULL terminate keyphrase, so every time you printf you overflow.
Also, to avoid the memory leak, simply replace char *keyphrase = (char *)malloc(7); with char keyphrase[8];.
You're calling malloc on the first line but I don't see a single free to release what you allocated. After all the loops are completed (i.e. usage of the allocated data is complete) you must call free(keyphrase);
New answer as the program has been modified.
You say your program is using all memory 'because I'm watching the available memory go down from over 4 free GB to about 5 MB'.
I'm guessing the answer is not the loops but these lines:
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
We can't see the source to or declaration of EncryptDES, but you aren't passing a length to it. If 0 is meant to be the length, this would explain it.
The next line however suggests that output is meant to be an array of 64 bytes (rather than 2 strings). But EncryptDES would have no way of knowing this.
I suggest you run the whole thing under valgrind to find out what is happening.
I am trying to read a series of 8 integers from a file into an array then display those integers. I keep getting a segmentation fault the third time around, and I can't quite figure out what I am doing wrong.
struct aStruct {
int a;
int b;
...
};
typedef struct aStruct myStruct;
while(fgets(line, MAX_LENGTH, file) != NULL) {
int myArray[8] = {0};
char* val = strtok (line," ,\n\t\r");
while (val != NULL)
{
myArray[i] = atoi(val);
i++;
val = strtok (NULL, " ,\n\t\r");
}
myStruct foo;
foo.a = myArray[0];
foo.b = myArray[1];
...
}
The input file is structured like so:
0, 0, 1, 5, 0, 0, 0, 0
1, 0, 2, 5, 0, 0, 0, 0
2, 0, 3, 5, 0, 0, 0, 0
3, 0, 4, 5, 0, 0, 0, 0
4, 0, 5, 5, 0, 0, 0, 0
When tested with:
printf("myArray[0]: %d ", myArray[0]);
I get an odd output of 0 0
Where I believe it should be 0 1. Am I not initializing something correctly, or is my new syntax incorrect for the struct? I've tried a few different combinations, cant quite figure it out.
I think your problem here is in uninitialized or non reset i variable. Adding i = 0 inside your while-loop might help.
while(fgets(line, MAX_LENGTH, file) != NULL) {
i = 0; // <<< reseting array index
int myArray[8] = {0};
char* val = strtok (line," ,\n\t\r");
while (val != NULL)
{
//...
i++;
}
}
When the Arduino is powered up it has an int array stored in the flash, for example:
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
When the program button is pressed, it then waits for the piezo to pick up a knock and this array then changes to, for example:
int secretCode[maximumKnocks] = {25, 50, 25, 50, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
(based on http://grathio.com/assets/secret_knock_detector.pde)
How would I write and read the array to/from the EEPROM? This is completely new to me, so any help would be great.
You would write the values using the EEPROM.Write function - loop over the array, writing each value in turn.
Assuming you don't need to store integer values > 254 ( in which case you'd have to write two bytes for each element in secretCode ), this would be:
for ( int i = 0; i < maximumKnocks; ++i )
EEPROM.write ( i, secretCode [ i ] );
Having written them, you would read them back on start-up using the read function in the setup. If the values in the EEPROM are 0xff, which they will be when you first flash the chip, don't copy them into the secret code.
if ( EEPROM.read ( 0 ) != 0xff )
for (int i = 0; i < maximumKnocks; ++i )
secretCode [ i ] = EEPROM.read ( i );