From: Eddards on 2 Oct 2009 11:47 I finally got it to work, and quick too. for (i=0; i<buffer.GetSize(); i++){ if (buffer[i] == 0x00) buffer[i] = 0x01; } CString CipStr = (LPCTSTR)buffer.GetData(); Thanks all "Tom Serface" <tom(a)camaswood.com> wrote in message news:55B1BC09-2883-4E7F-875C-25059F1C02F7(a)microsoft.com... > Shoot, you're right. CString, doesn't have a SetSize() method. :o( I > was sure that would work... The size for CString is determined by the > null at the end. There is a GetLength() of course, but no was to set the > size like other types of arrays. I should have checked the help first. > Apologies. > > What if you tried the form of CString constructor where it is prefilled to > a size using a known character. > > CString CipStr(_T('x'),buffer.GetSize()); > > I'm not sure that will make it any faster, but it may make it allocated > the values for the string in one allocation call rather than resizing it > for each character you add. > > Worth a try anyway. > > You could aslo try getting a pointer to the buffer, resizing it, using the > pointer to put in your own characters then calling ReleaseBuffer() to put > it back into the CString. > > Tom > > > "Eddards" <eddards(a)verizon.net> wrote in message > news:SJWdnQfW-8IbnFvXnZ2dnUVZ_sadnZ2d(a)giganews.com... >> Compiler error: >> error C2039: 'SetSize' : is not a member of 'CString' >> >> >> "Mikel" <mikel.luri(a)gmail.com> wrote in message >> news:f6b09929-7949-478a-a434-324702efa30c(a)j4g2000yqa.googlegroups.com... >> Is it over a minute in Release mode or in Debug mode? >> Anyway, for such a long buffer, I think you should set the size of the >> destination string before starting to add elements. Otherwise, the >> string will have to reallocate the memory as it grows, which can take >> a lot of time. >> >> Try something like this: >> >> CString CipStr; >> CipStr.SetSize(buffer.GetSize()); >> >> for (i=0; i<buffer.GetSize(); i++) >> { >> if (buffer[i] == 0x00) >> buffer[i] = 0x01; >> CipStr.SetAtGrow(i, buffer[i]); // You could use CipStr[i] = buffer >> [i] or CipStr.SetAt(i, buffer[i]), but just in case... >> } >> >> Besides, take into account that you are changing the data in the >> original buffer, and maybe that's no what you want... >> >> On 2 oct, 14:23, "Eddards" <edda...(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" <edda...(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" <newco...(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" <edda...(a)verizon.net> >>> >> wrote: >>> >>> >>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying >>> >>>to >>> >>>the >>> >>>CString >>> >>>Is this possible ? >>> >>> >>>"Eddards" <edda...(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: newco...(a)flounder.com >>> >> Web:http://www.flounder.com >>> >> MVP Tips:http://www.flounder.com/mvp_tips.htm- Ocultar texto de la >>> >> cita - >>> >>> - Mostrar texto de la cita - >> >> >
From: Joseph M. Newcomer on 2 Oct 2009 12:30 See below... On Fri, 2 Oct 2009 11:47:11 -0400, "Eddards" <eddards(a)verizon.net> wrote: >I finally got it to work, and quick too. > > for (i=0; i<buffer.GetSize(); i++){ > if (buffer[i] == 0x00) > buffer[i] = 0x01; > } > CString CipStr = (LPCTSTR)buffer.GetData(); **** This is incorrect. buffer is NOT an LPCTSTR, therefore this cast is erroneous. It would NEVER make sense to have the buffer treated as an LPCTSTR, because the buffer is necessarily LPCSTR (8-bit characters) and you are therefore telling the compiler, in a Unicode build, that it should treat a sequence of 8-bit characters (and you have not said that you guarantee the buffer is NUL-terminated!) as a sequence of Unicode characters! If this works, it is an accident waiting to become a disaster As with most questions, it was inadequately asked and critical information was omitted. The correct question would have been "What is the best way to read a file of ~4MB of 8-bit characters into a CString. In addition to reading the file into the string, I need to replace internal 0x00 characters with 0x01. The application is ANSI, written in VS6, and will never be Unicode". You also did not say how you are reading your file: CFile, FILE*, etc. I would use CFile. In that case, the answer would have been int size = file.GetLength(); // or however you determine the file size CString buffer; LPSTR p = buffer.GetBuffer(size + 1); file.Read(p, size); p[size] = '\0'; for(int i = 0; i < size; i++) if(p[i] == 0x00) p[i] = 0x01; buffer.ReleaseBuffer(); if you are not using CFile, replace the appropriate pieces with code to get the length of the file, and read the data. Note that this is close to the most efficient mechanism you can use. There might be a way to exploit the internally optimized strlen code to find each NUL character slightly faster than the array scan. Note this requires NO copy into the string. joe > >Thanks all > > >"Tom Serface" <tom(a)camaswood.com> wrote in message >news:55B1BC09-2883-4E7F-875C-25059F1C02F7(a)microsoft.com... >> Shoot, you're right. CString, doesn't have a SetSize() method. :o( I >> was sure that would work... The size for CString is determined by the >> null at the end. There is a GetLength() of course, but no was to set the >> size like other types of arrays. I should have checked the help first. >> Apologies. >> >> What if you tried the form of CString constructor where it is prefilled to >> a size using a known character. >> >> CString CipStr(_T('x'),buffer.GetSize()); >> >> I'm not sure that will make it any faster, but it may make it allocated >> the values for the string in one allocation call rather than resizing it >> for each character you add. >> >> Worth a try anyway. >> >> You could aslo try getting a pointer to the buffer, resizing it, using the >> pointer to put in your own characters then calling ReleaseBuffer() to put >> it back into the CString. >> >> Tom >> >> >> "Eddards" <eddards(a)verizon.net> wrote in message >> news:SJWdnQfW-8IbnFvXnZ2dnUVZ_sadnZ2d(a)giganews.com... >>> Compiler error: >>> error C2039: 'SetSize' : is not a member of 'CString' >>> >>> >>> "Mikel" <mikel.luri(a)gmail.com> wrote in message >>> news:f6b09929-7949-478a-a434-324702efa30c(a)j4g2000yqa.googlegroups.com... >>> Is it over a minute in Release mode or in Debug mode? >>> Anyway, for such a long buffer, I think you should set the size of the >>> destination string before starting to add elements. Otherwise, the >>> string will have to reallocate the memory as it grows, which can take >>> a lot of time. >>> >>> Try something like this: >>> >>> CString CipStr; >>> CipStr.SetSize(buffer.GetSize()); >>> >>> for (i=0; i<buffer.GetSize(); i++) >>> { >>> if (buffer[i] == 0x00) >>> buffer[i] = 0x01; >>> CipStr.SetAtGrow(i, buffer[i]); // You could use CipStr[i] = buffer >>> [i] or CipStr.SetAt(i, buffer[i]), but just in case... >>> } >>> >>> Besides, take into account that you are changing the data in the >>> original buffer, and maybe that's no what you want... >>> >>> On 2 oct, 14:23, "Eddards" <edda...(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" <edda...(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" <newco...(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" <edda...(a)verizon.net> >>>> >> wrote: >>>> >>>> >>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying >>>> >>>to >>>> >>>the >>>> >>>CString >>>> >>>Is this possible ? >>>> >>>> >>>"Eddards" <edda...(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: newco...(a)flounder.com >>>> >> Web:http://www.flounder.com >>>> >> MVP Tips:http://www.flounder.com/mvp_tips.htm- Ocultar texto de la >>>> >> cita - >>>> >>>> - Mostrar texto de la cita - >>> >>> >> > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Tom Serface on 2 Oct 2009 18:19 I'm glad this works for you, but Joe's concerns are also reasonable. If the data in the CByteArray are all characters (and since you're change the nulls to 0x01) you can probably get away with casting the data into the string. I'm nto sure what you get from this since it's just another representation of the data. I don't know the whole story of course :o) You'll want to do a lot of test with various data to make sure you don't run into any glitches with the approach. Tom "Eddards" <eddards(a)verizon.net> wrote in message news:3IOdnSgQItnivVvXnZ2dnUVZ_rednZ2d(a)giganews.com... >I finally got it to work, and quick too. > > for (i=0; i<buffer.GetSize(); i++){ > if (buffer[i] == 0x00) > buffer[i] = 0x01; > } > CString CipStr = (LPCTSTR)buffer.GetData(); > > Thanks all
First
|
Prev
|
Pages: 1 2 Prev: AfxConnectionAdvise: No events fired Next: arrangement of include files |