Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

CString to byte array at specific array index

Posted on 2007-07-25
5
Medium Priority
?
831 Views
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;

memset(bArrTemp,0x00,18);

memcpy(&bArrTemp[2],szWrite,szWrite.GetLength());

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

0
Comment
Question by:howardsd
  • 2
  • 2
5 Comments
 

Author Comment

by:howardsd
ID: 19570242
My mistake, it's actually tacking on a lot of extra bytes of gibberish.
0
 
LVL 22

Expert Comment

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

0
 
LVL 86

Accepted Solution

by:
jkr earned 375 total points
ID: 19571353
>>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);

memcpy(&bArrTemp[2],szWrite,szLen);

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

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

Expert Comment

by:jkr
ID: 19571355
BTW, why aren't you simply using

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

instead?
0
 

Author Comment

by:howardsd
ID: 19576115
It was the damn null terminator I wasn't copying.  Duh.

Thnx!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

580 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