This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

"problems" with strcat

Hello Everybody
I want to attach one String to another.
The String that I want to attach is for example: {0x01,0x02,0x03,0x00,0x05}

the strcat doesn't copy the bytes after 0x03 because it detects 0x00 as the NUL-Terminator I guess.

its the same when I use strncat with the right length parameter.

...but I really need to handle strings like that!

Who can help?

Thanx a lot

Parents

  • If the data in your arrays has no predictable terminator value (like the null character in C strings), then you'll have to remember the length of the data.

    To concatenate, all you have to do is a bit of pointer arithmetic on your destination.

    int memcat (U8* dst, int dstLen,
                U8* src, int srcLen)
        {
        return memcpy (dst + dstlen, src, srcLen);
        }
    

    Note that you'd better be sure that dst points to a buffer big enough to hold all this data.

    Also remember that adding '1' to a pointer in C adds not one byte to the address, but sizeof(TypePointedTo). So if you have

    typedef struct
       {
       ... // various fields here
       } Header;
    
    Header* hdr;
    

    then to point just past the header, you'd say "hdr + 1", not "hdr + sizeof(Header)". Of course, if your TypePointedTo happens to be a byte, then "+ 1" does what you might have expected.

    If you like, you can gussy up the basic concept by putting the variables together in a structure.

    typedef struct
       {
       int maxLen;
       int curLen;
       U8* data;
       } BoundedArray;
    

    and then write your routines to pass those structures around to appropriate functions built on top of memcpy().

Reply

  • If the data in your arrays has no predictable terminator value (like the null character in C strings), then you'll have to remember the length of the data.

    To concatenate, all you have to do is a bit of pointer arithmetic on your destination.

    int memcat (U8* dst, int dstLen,
                U8* src, int srcLen)
        {
        return memcpy (dst + dstlen, src, srcLen);
        }
    

    Note that you'd better be sure that dst points to a buffer big enough to hold all this data.

    Also remember that adding '1' to a pointer in C adds not one byte to the address, but sizeof(TypePointedTo). So if you have

    typedef struct
       {
       ... // various fields here
       } Header;
    
    Header* hdr;
    

    then to point just past the header, you'd say "hdr + 1", not "hdr + sizeof(Header)". Of course, if your TypePointedTo happens to be a byte, then "+ 1" does what you might have expected.

    If you like, you can gussy up the basic concept by putting the variables together in a structure.

    typedef struct
       {
       int maxLen;
       int curLen;
       U8* data;
       } BoundedArray;
    

    and then write your routines to pass those structures around to appropriate functions built on top of memcpy().

Children
No data