Im trying to convert the specific substring that I extract it from a given string to type Bytes in an array, Byte for is unit8_t , so every 8 bits in the substring I want to convert them to type Byte, the output is an array of type Byte corresponded to the extracted substring, if the substring is appeared in a given string twice (more than one, maybe three times ..etc) then I should return an array two dimensions (matrix) that every row is an array of type BYTE correspond to the extracted substring .. for example if I have three times occurrences of my substring in a given string, then the output should be a two dimensions array type Byte, first row of the matrix is an array of type Byte correspond to first occurrence of substring, second row is an array of type Byte corresponded to second occurrence of substring , etc ..
Im clarifying more, lets assume I have this given string:
char* str[]="111000000001010101011100000000101010001110000000 010100000";
so for instance my specific substring that I search is char* substring[]="111" ; so I should return the substring that starts from last index of my specific string + 16 steps =>
so my extracted specific string are:
for first occurrence of "111" (last index here of "111" is i=2, so I extract the substring from i=3 till i=18, in other words I jump every time 16 steps, so the length of my extracted string is 18-3+1 = 16) : "0000000010101010"
for second occurrence of "111" (same as above but last index here of "111" is i=21 ):"0000000010101000"
for third occurrence of "111"(same as above but last index here of "111" is i=40):"0000000010100000"
so I want to take every extracted string and convert it to array of type Byte, this means every extracted string I must divide it by 8 (8 bits) and convert it to Byte, for instance for first extracted string:
"0000000010101010" the output is ..this because first "00000000" in Byte type it's 0 , "10101010" in Byte type it's 170, so the output is ...the same for other extracted substrings..So the final output should be an array of two dimension that looks like
Code:
{{0,170}, {0,168} , ..}
, the returned value is an array of two dimensions.
I have done two functions in C/C++ programming:
first function that convert any string/substring to array type Byte.
second function that returns the extracted substrings with given specific substring.<br/>
Here's my code for first function that extracts the substrings:
Code:
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using Match = std::pair<std::string::size_type, std::string>;
using MatchVec = std::vector<Match>;
MatchVec findPattern(const std::string &str, const std::string &pattern)
{
MatchVec vec;
Match m;
std::string::size_type index, pos = 0;
while ((index = str.find(pattern, pos)) != std::string::npos) {
m.first = index;
index += pattern.length();
m.second = str.substr(index, 6);
vec.push_back(m);
pos = index;
}
return vec;
}
int main()
{
std::string str1 = "111000000001010101011100000000101010001110000000010100000";
std::string str2 = "111";
auto matches = findPattern(str1, str2);
for(auto &match : matches) {
std::cout << "Match found at position: " << match.first << std::endl;
std::cout << "String is " << match.second << std::endl;
}
return 0;
}
second function that converts the substrings/strings is:
Code:
#include <stdio.h>
#include <string.h> // strcpy, strtok
// argument 2 is an 2-dimensional array where the tokens are copied into
// Return-Value: number of tokens where found
int mixed(char *var, char *delimiter, char numbers[20][60])
{
printf ("the delimiter is: %s(space)\n", delimiter);
int retval = 0, i = 0;
char * d;
d = strtok (var, delimiter);
while (d != NULL)
{
strcpy(numbers[i], d);
//printf ("i: %2d String: %s\n", i, numbers[i]);
i++;
d = strtok (NULL, delimiter);
retval++;
}
return retval;
}
// copies byte start to end form string allchars to string result_string
void copychars(char allchars[], char result_string[], int start, int end)
{
int i, j = 0;
for (i = start; i <= end; i++)
{
result_string[j] = allchars[i];
result_string[j + 1] = '\0';
j++;
}
}
/* This function bintodec gets the decimal value of an 8 Byte long binary value
* The first loop is to get length of given string 'wort'
* ----------------------------------------------------------------------- */
void bintodec(unsigned int (*decval), char wort[])
{
unsigned int i, ende, dumsm, dumza = 0, testvalue = 1;
for (ende = 0; ende <= 8; ende++)
if (wort[ende] == '\0') break;
testvalue <<= (ende - 1); /* sets testvalue according to length of string */
for (i = 0; i <= (ende - 1); i++)
if ((wort[i] == 'I') || (wort[i] == '1'))
{
dumsm = testvalue >> i;
dumza += dumsm;
}
(*decval) = dumza;
}
int main(int argc, char **argv)
{
int rxlen = -1, i, j, k, start, end;
int number_of_tokens = 0;
char delimiter[] = " ";
char rxbuffer_all[]="111000000001010101011100000000101010001110000000010100000";
char parts[6][60] = {0};
//char bitvalue[12] = {0};
char bitsofchar[5][6][12] = {0};
unsigned int decvalues[5][6] = {0};
printf("given was this string:\n%s\n", rxbuffer_all);
rxlen = strlen(rxbuffer_all);
printf("Length of given string is: %d byte\n", rxlen);
number_of_tokens = mixed(rxbuffer_all, delimiter, parts);
printf ("\nNumber of tokens: %d\n-----tokens splited into characters-------\n", number_of_tokens);
for (i = 0; i < number_of_tokens; i++)
{
rxlen = strlen(parts[i]);
printf("token nr: %2d Token=%s length=%d\n", i, parts[i], rxlen);
}
// Now separate tokens into binary values shown as 8-char-string
// and put this binay values into int-values
for (k = 0; k < 5; k++)
{
printf("\nToken number %d\n", k);
for (i = 0, j = 0; i <= 48; i+= 8, j++)
{
start = i;
end = i + 7;
copychars(parts[k], bitsofchar[k][j], start, end);
if (strlen( bitsofchar[k][j]) == 0) break;
bintodec(&decvalues[k][j], bitsofchar[k][j]);
printf("j=%2d start=%2d end=%2d String=%s decimal value: %u\n", j, start, end, bitsofchar[k][j], decvalues[k][j]);
}
}
return 0;
}
**Im not succeeding to make the integration between them and output the final output which it's a two denominational array, could please anyone help me how could I implement that in order to get the required output? I struggled for that much time but still no progress, I know that second function in C (Im not succeeding in C++ to make this) , preferable solution for my problem is in C++ , thanks alot for any assistance!
Note, the output of my problem is an array type Byte for every extracted substring according to what I explained above .. **