Concating byte[] and char[]

Discussion in 'C' started by Snake, Jan 26, 2010.

  1. Snake

    Snake New Member

    Joined:
    Jan 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi there!
    Iam writing a piece of code that will send file (using Wininet) to PHP script for uploading.
    Headers with boundary, filename and other stuff stored as char[]. But file content that i must send to script is in byte array. How do i concat chars and bytes array? Binary data will be particullary cut (cause of 00's). Also, is there a function to figure the byte array length?
    Thanks =)
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    There is no "byte" type in C/C++ Some compilers and/or platform SDKs define or typedef it to "unsigned char" Casting your char array to "unsigned char *" should be OK in most cases.

    There's no way to get byte array length except to keep track of it with an additional variable, (or use a std container like vector<> ). There is no NULL terminator like there is for a C string (char array).
     
  3. Snake

    Snake New Member

    Joined:
    Jan 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thnx for reply!
    iam using c along with winapi and i need to concat byte and char strings. But i can't concat them with C string functions, cause when i do it byte buffer is being cut to null-terminant. So what should i do?
    Sorry for my lameness =)
     
  4. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Typically, you'd use memcpy(). It takes an additional parameter that tells it how many bytes to copy.

    Code:
    
      //three unequal length buffers
      unsigned char buf1[26];
      size_t buf1len=26;
    
      unsigned char buf2[114];
      size_t buf2len=114;
    
      unsigned char buf3[54];
      size_t buf3len=54;
    
      //fill buffers, etc.
    
      unsigned char copybuf[500];  //or could be allocated with malloc or new
    
      memcpy(copybuf,buf1,buf1len);
      memcpy(copybuf+buf1len,buf2,buf2len);
      memcpy(copybuf+buf1len+buf2len,buf3,buf3len);
    
    
     
  5. Snake

    Snake New Member

    Joined:
    Jan 26, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    much appreciate =)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice