How to 'replicate' an array of hex values?

This is a discussion on How to 'replicate' an array of hex values? within the C Programming forums, part of the General Programming Boards category; I have a set of 4 fixed bytes that i put in an array: Code: char fixed[] = "\xcd\xf1\x12\xbf"; Then ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    Question [SOLVED]How to 'replicate' an array of hex values?

    I have a set of 4 fixed bytes that i put in an array:

    Code:
    char fixed[] = "\xcd\xf1\x12\xbf";
    Then I want to fill another newarray[1028] with the values of the array above.
    (ie 257 X fixed[] inside newarray[1028]) and get something like:

    Code:
    char newarray[1028] = "\xcd\xf1\x12\xbf\xcd\xf1\x12\xbf\xcd\xf1\x12\xbf.........";
    I tried with an ugly loop but I doesn't seem to work:

    Code:
    int i, j = 0;
    char fixed[] = "\xcd\xf1\x12\xbf";
    char newarray[1028];
    for(i=0;i<1028;i++) {
        if(j > 3)
            j = 0;
        newarray[i] = fixed[j];
        j++;
    }
    Any idea how I can 'replicate' an array n times in another array?
    Last edited by sef0; 05-27-2009 at 08:54 AM. Reason: solved, close thread

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    You need to test 'j' after the assignment. Also, you could simply use memcpy in a loop.

    EDIT: Actually, the code posted should work (unlike my brain at the moment).
    Last edited by Sebastiani; 05-27-2009 at 07:07 AM.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    but if I do:

    Code:
    for(i=0;i<1028;i++) {
        printf("%02x",newarray[i]);
    }
    I don't get cdf112bf cdf112bf cdf112bf cdf112bf ... 257 times

    How would I use memcpy() to replicate my array?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sef0 View Post
    but if I do:

    Code:
    for(i=0;i<1028;i++) {
        printf("%02x",newarray[i]);
    }
    I don't get cdf112bf cdf112bf cdf112bf cdf112bf ... 257 times

    How would I use memcpy() to replicate my array?
    No, I expect that your compiler (like 95% of all compilers) have signed char, which means that your 0xcd and 0xf1 becomes fffffffcd and fffffff1 respectively. If you make either cast to unsigned char in the printf or make newarray an unsigned char in the first place, then I expect you get the right values.

    A loop calling memcpy() for 4 bytes at a time is probably slower than the for loop suggested in the original post - and if the string is only 1028 bytes ever, then I can't for my life see any benefit in optimizing it [unless you have a 2MHz 4-bit processor].

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    >> I don't get cdf112bf cdf112bf cdf112bf cdf112bf ... 257 times

    It would actually be 256 times, but anyway, what output are you getting?

    >> How would I use memcpy() to replicate my array?

    Something like:

    Code:
    char* duplicate( char* buffer, size_t buffer_length, char const* pattern, size_t pattern_length )
    {
        for( char* ptr = buffer; buffer_length != 0; buffer_length -= pattern_length, ptr += pattern_length )
        {
            if( pattern_length > buffer_length )
                pattern_length = buffer_length;
            memcpy( ptr, pattern, pattern_length );
        }
        return buffer;
    }
    EDIT:
    OK, so it wasn't really any simpler than the original code.
    Last edited by Sebastiani; 05-27-2009 at 07:38 AM.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void rep( char *buf, size_t bufsiz, char *pat, size_t patsiz )
    {
        size_t x = 0, y = 0;
        while( x < bufsiz )
            buf[ x++ ] = pat[ y++ % patsiz ];
    }
    How about that?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,702
    Yes, much better.
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Only if the processor is clever enough to perform the % operator using AND (meaning (patsiz & (patsiz-1)) == 0) - otherwise, I expect if (y >= patsiz) y = 0; will be faster.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    5
    Actually, matsp, you were absolutely correct when you pointed me to the 'unsigned'.
    It fixed my problem!

    Code:
    unsigned char fixed[] = "\xcd\xf1\x12\xbf";
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21