CString to byte array at specific array index

Posted on 2007-07-25
Last Modified: 2008-01-09
I need to put a CString into a byte array at a specific index.  I've tried the below method, but it seems to tack on 2 extra bytes of gibberish to the end of the array.

BYTE bArrTemp[18];
CString szWrite;



bArrTemp[0] = TAG_GUID;
bArrTemp[1] = LEN_GUID;

Question by:howardsd

    Author Comment

    My mistake, it's actually tacking on a lot of extra bytes of gibberish.
    LVL 22

    Expert Comment

    what's the possible length of the  CString?  if it's more than 15, you'll have a problem.

    LVL 86

    Accepted Solution

    >>I've tried the below method, but it seems to tack on 2 extra bytes of gibberish
    >>to the end of the array.

    The way you are using that, the NULL terminator for the string might be missing. I'd rather use

    #include <tchar.h>


    size_t stLen = szWrite.GetLength() * sizeof(TCHAR);

    BYTE* bArrTemp = new BYTE[szLen + 1] ;
    CString szWrite;

    memset(bArrTemp,0x00,szLen + 1);


    bArrTemp[0] = TAG_GUID;
    bArrTemp[1] = LEN_GUID;

    // delete [] bArrTemp; later when you no longer need it.
    LVL 86

    Expert Comment

    BTW, why aren't you simply using

    szWrite[0] = TAG_GUID;
    szWrite[1] = LEN_GUID;


    Author Comment

    It was the damn null terminator I wasn't copying.  Duh.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Some Windows API functions expect you to provide a pointer to a CALLBACK function that the system will need to call as part of the operation.  Such API functions as SetTimer, timeSetEvent, CreateThread, EnumWindows, LineDDA, even window message hand…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now