From: Tom Serface on
Try something like:

CString myString;

LPTSTR p = myString.GetBuffer( buffer.GetSize() +1 );

loop through p and buffer add your characters

myString.ReleaseBuffer();

Tom

"Eddards" <eddards(a)verizon.net> wrote in message
news:9Y-dnUoO85ysiVvXnZ2dnUVZ_gednZ2d(a)giganews.com...
> OK so now I have the following:
>
> for (i=0; i<buffer.GetSize(); i++){
> if (buffer[i] == 0x00)
> buffer[i] = 0x01;
> }
> CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize());
>
> Compiler error:
> error C2064: term does not evaluate to a function
>
>
> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
> news:op2cc51p1oorq1s13h5g4us37ktgauv5nm(a)4ax.com...
>> Because you did something truly silly: :you did +=. This means the
>> second time through,
>> it has to make a copy of a 1-character ClipStr; the third time through it
>> has to make a
>> copy of a 2-ClipStr ClipStr, the fourth time through it has to make a
>> copy of a
>> 3-character ClipStr, and each time it goes through it has to make a copy!
>> This is called
>> the "exponential copy problem", and you must have a very fast machine if
>> it only took a
>> minute. By the 4,200,001 iteration it was copying a string of 4,200,000
>> characters, then
>> appending a character to that, then copying 4,200,001 characters and
>> appending a
>> character to that, and so on. This requires an allocation, which must be
>> initialized,
>> then a massive copy, only to, a few nanoseconds later, repeat this
>> lengthy process.
>>
>> The CORRECT code would have been to run through and change all the bytes,
>> and THEN make
>> PRECISELY ONE copy, at the end!
>>
>> Note that you also failed in describing a problem: you didn't give all
>> the critical
>> information! String length is a critical parameter!
>>
>> for(int i = 0; i < buffer.GetSize(); i++)
>> {
>> if(buffer[i] = 0x00)
>> buffer[i] = 0x01;
>> }
>>
>> CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize());
>>
>> This makes only one copy.
>> joe
>>
>> On Fri, 2 Oct 2009 08:23:49 -0400, "Eddards" <eddards(a)verizon.net> wrote:
>>
>>>This is what I have.
>>>The size of the file read into buffer was 4.6mb
>>>Why does it take so long to run through? (over a minute).
>>>
>>> int i;
>>> CString CipStr;
>>> for (i=0; i<buffer.GetSize(); i++){
>>> if (buffer[i] == 0x00)
>>> buffer[i] = 0x01;
>>> CipStr += buffer[i];
>>> }
>>>
>>>
>>>"Eddards" <eddards(a)verizon.net> wrote in message
>>>news:KpidnfDBIMVReljXnZ2dnUVZ_uOdnZ2d(a)giganews.com...
>>>>I tried copying the buffer with a loop like you show below.
>>>> It took over a minute to process all 4,000,000 bytes.
>>>> Dont know why it should take so long.
>>>>
>>>>
>>>> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>>>> news:57sac5hc63e673ijs0fdttud2annvadrno(a)4ax.com...
>>>>> for(int i = 0; i < buffer.GetSize(); i++)
>>>>> if(buffer[i] == 0x00)
>>>>> buffer[i] = 0x01;
>>>>>
>>>>> What is so hard about this? This is pretty elementary C programming!
>>>>>
>>>>> If you want to do it more along the "hard way" read about the
>>>>> MultiByteToWideChar API (and
>>>>> see my essay on my MVP Tips site on CString techniques)
>>>>>
>>>>> joe
>>>>> On Thu, 1 Oct 2009 15:09:07 -0400, "Eddards" <eddards(a)verizon.net>
>>>>> wrote:
>>>>>
>>>>>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying to
>>>>>>the
>>>>>>CString
>>>>>>Is this possible ?
>>>>>>
>>>>>>"Eddards" <eddards(a)verizon.net> wrote in message
>>>>>>news:9fGdnWZaLPg3J1nXnZ2dnUVZ_u6dnZ2d(a)giganews.com...
>>>>>>>I am using VC6.
>>>>>>> Is there a way to copy a CByteArray to a CString?
>>>>>>>
>>>>>>>
>>>>>>
>>>>> Joseph M. Newcomer [MVP]
>>>>> email: newcomer(a)flounder.com
>>>>> Web: http://www.flounder.com
>>>>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>>>
>>>>
>>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
>

From: Giovanni Dicanio on


"Eddards" <eddards(a)verizon.net> ha scritto nel messaggio
news:9Y-dnUoO85ysiVvXnZ2dnUVZ_gednZ2d(a)giganews.com...
> OK so now I have the following:
>
> for (i=0; i<buffer.GetSize(); i++){
> if (buffer[i] == 0x00)
> buffer[i] = 0x01;
> }
> CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize());
>
> Compiler error:
> error C2064: term does not evaluate to a function

I second the idea of using CString::GetBuffer/ReleaseBuffer.

You may want to try this code, which seems to compile and work fine:

<code>
CByteArray buffer;
... fill the buffer or something ...

// Index for buffer looping
int i;

// Change 0x00 bytes in buffer to 0x01
for (i = 0; i < buffer.GetSize(); i++)
{
if (buffer[i] == 0x00)
buffer[i] = 0x01;
}

// Copy buffer content to a CString instance
CString str;
LPTSTR psz = str.GetBuffer( buffer.GetSize() + 1 );
for (i = 0; i < buffer.GetSize(); i++)
{
psz[i] = buffer[i];
}
psz[i] = 0; // end of string
str.ReleaseBuffer();

// Show the string
AfxMessageBox(str);

</code>

Note that we are just raw-copying the byte array content to the CString, but
you should be sure about the encoding of the text data stored in the byte
array (is this ANSI? Or UTF-8? Or some other code page?)

Giovanni


From: Joseph M. Newcomer on
I'm sure that error message has a line associated with it...

It has been years since I used VS6, but I fired it up and used the code shown below,
modified the project settings to compile as Unicode, and looked at the memory results:

CByteArray buffer;
buffer.SetSize(4);
buffer[0] = 'A';
buffer[1] = 'B';
buffer[2] = 'C';
buffer[3] = 'D';
CString s((LPCSTR)buffer.GetData(), buffer.GetSize());

buffer.GetData():
00326D78 41 42 43 44 FD FD FD FD 0D
s.GetBuffer():
00326DCC 41 00 42 00 43 00 44 00 00

So it compiles and executes correctly.
joe

On Fri, 2 Oct 2009 10:54:42 -0400, "Eddards" <eddards(a)verizon.net> wrote:

>OK so now I have the following:
>
> for (i=0; i<buffer.GetSize(); i++){
> if (buffer[i] == 0x00)
> buffer[i] = 0x01;
> }
> CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize());
>
>Compiler error:
>error C2064: term does not evaluate to a function
>
>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>news:op2cc51p1oorq1s13h5g4us37ktgauv5nm(a)4ax.com...
>> Because you did something truly silly: :you did +=. This means the second
>> time through,
>> it has to make a copy of a 1-character ClipStr; the third time through it
>> has to make a
>> copy of a 2-ClipStr ClipStr, the fourth time through it has to make a copy
>> of a
>> 3-character ClipStr, and each time it goes through it has to make a copy!
>> This is called
>> the "exponential copy problem", and you must have a very fast machine if
>> it only took a
>> minute. By the 4,200,001 iteration it was copying a string of 4,200,000
>> characters, then
>> appending a character to that, then copying 4,200,001 characters and
>> appending a
>> character to that, and so on. This requires an allocation, which must be
>> initialized,
>> then a massive copy, only to, a few nanoseconds later, repeat this lengthy
>> process.
>>
>> The CORRECT code would have been to run through and change all the bytes,
>> and THEN make
>> PRECISELY ONE copy, at the end!
>>
>> Note that you also failed in describing a problem: you didn't give all the
>> critical
>> information! String length is a critical parameter!
>>
>> for(int i = 0; i < buffer.GetSize(); i++)
>> {
>> if(buffer[i] = 0x00)
>> buffer[i] = 0x01;
>> }
>>
>> CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize());
>>
>> This makes only one copy.
>> joe
>>
>> On Fri, 2 Oct 2009 08:23:49 -0400, "Eddards" <eddards(a)verizon.net> wrote:
>>
>>>This is what I have.
>>>The size of the file read into buffer was 4.6mb
>>>Why does it take so long to run through? (over a minute).
>>>
>>> int i;
>>> CString CipStr;
>>> for (i=0; i<buffer.GetSize(); i++){
>>> if (buffer[i] == 0x00)
>>> buffer[i] = 0x01;
>>> CipStr += buffer[i];
>>> }
>>>
>>>
>>>"Eddards" <eddards(a)verizon.net> wrote in message
>>>news:KpidnfDBIMVReljXnZ2dnUVZ_uOdnZ2d(a)giganews.com...
>>>>I tried copying the buffer with a loop like you show below.
>>>> It took over a minute to process all 4,000,000 bytes.
>>>> Dont know why it should take so long.
>>>>
>>>>
>>>> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>>>> news:57sac5hc63e673ijs0fdttud2annvadrno(a)4ax.com...
>>>>> for(int i = 0; i < buffer.GetSize(); i++)
>>>>> if(buffer[i] == 0x00)
>>>>> buffer[i] = 0x01;
>>>>>
>>>>> What is so hard about this? This is pretty elementary C programming!
>>>>>
>>>>> If you want to do it more along the "hard way" read about the
>>>>> MultiByteToWideChar API (and
>>>>> see my essay on my MVP Tips site on CString techniques)
>>>>>
>>>>> joe
>>>>> On Thu, 1 Oct 2009 15:09:07 -0400, "Eddards" <eddards(a)verizon.net>
>>>>> wrote:
>>>>>
>>>>>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying to
>>>>>>the
>>>>>>CString
>>>>>>Is this possible ?
>>>>>>
>>>>>>"Eddards" <eddards(a)verizon.net> wrote in message
>>>>>>news:9fGdnWZaLPg3J1nXnZ2dnUVZ_u6dnZ2d(a)giganews.com...
>>>>>>>I am using VC6.
>>>>>>> Is there a way to copy a CByteArray to a CString?
>>>>>>>
>>>>>>>
>>>>>>
>>>>> Joseph M. Newcomer [MVP]
>>>>> email: newcomer(a)flounder.com
>>>>> Web: http://www.flounder.com
>>>>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>>>
>>>>
>>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm