Thread: Converting all extracted substrings to array type Byte

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    50

    Converting all extracted substrings to array type Byte

    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
    Code:
    {{0,170}}
    ..this because first "00000000" in Byte type it's 0 , "10101010" in Byte type it's 170, so the output is
    Code:
    {{0,170}}
    ...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 .. **
    Last edited by Brian_Teir; 07-26-2020 at 05:55 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,178
    Like this?
    Im clarifying more, lets assume I have this given string:


    /* 01234567890123456789012345678901234567890123456789 0123456 */
    char* str[]="111000000001010101011100000000101010001110000000010100000";

    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"
    OK, so when I change the 6 to 16 in this
    > m.second = str.substr(index, 16);

    I get
    Code:
    $ g++ -std=c++11 foo.cpp
    $ ./a.out 
    Match found at position: 0
    String is 0000000010101010
    Match found at position: 19
    String is 0000000010101000
    Match found at position: 38
    String is 0000000010100000
    What's the problem?

    Throw away all that C code, it's got nothing to do with the problem you now have.

    std::stoul, std::stoull - cppreference.com
    Code:
                std::cout << "Hex=" << std::hex << stoul(match.second,0,2) << std::endl;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    50
    the output should be like this:
    {{1,2,3,4},{6,7,8,9},{9,5,6,7}}

    two dimensional array that every row is an array type Byte corresponded to the extracted substring ..
    Last edited by Brian_Teir; 07-26-2020 at 08:28 AM.

  4. #4
    Registered User
    Join Date
    Nov 2019
    Posts
    50
    solved!
    Last edited by Brian_Teir; 07-26-2020 at 08:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting binary array to Byte array
    By Brian_Teir in forum C Programming
    Replies: 12
    Last Post: 07-21-2020, 05:49 PM
  2. Converting char array to int16 type
    By sanks85 in forum C Programming
    Replies: 2
    Last Post: 04-18-2013, 07:07 AM
  3. converting string to byte in c++
    By Klutz15 in forum C++ Programming
    Replies: 9
    Last Post: 06-14-2011, 07:54 AM
  4. Converting an array of byte to an array int
    By chrisfarrugia in forum C Programming
    Replies: 7
    Last Post: 03-05-2008, 08:30 AM
  5. Converting an 8-Byte Array into a Long Representation
    By maththeorylvr in forum C Programming
    Replies: 11
    Last Post: 06-17-2005, 03:21 PM

Tags for this Thread