Ok Can anyone get around this... 
Author Message
 Ok Can anyone get around this...

Hi,

Strugglin' late on a friday with drinkin afoot, does anyone know a neater
faster cheeper way to do this in MSVC++6

   CString DataStr;
   unsigned char Datachar[100];
   int i;
   for (i=0;i!=100;i++){
          Datachar[i]=DataStr[i];
   }

Cheers
Wal



Tue, 30 Jul 2002 03:00:00 GMT  
 Ok Can anyone get around this...
Neater (but probably not faster),

unsigned char Datachar[100];
CString DataStr;
DataStr.Format( "%s",  Datachar );

assumes only that the Datachar array
contains a valid zero-terminated string.
If it doesn't then there's not much point
in copying it into a CString....

-------------------------------------------------------------------
"If you think you know the answer,
then you don't understand the question !"
-------------------------------------------------------------------


Quote:
> Hi,

> Strugglin' late on a friday with drinkin afoot, does anyone know a neater
> faster cheeper way to do this in MSVC++6

>    CString DataStr;
>    unsigned char Datachar[100];
>    int i;
>    for (i=0;i!=100;i++){
>           Datachar[i]=DataStr[i];
>    }

> Cheers
> Wal



Tue, 30 Jul 2002 03:00:00 GMT  
 Ok Can anyone get around this...
He wanted to copy FROM a CString TO an unsigned char[].

NeilB


Quote:
> Neater (but probably not faster),

> unsigned char Datachar[100];
> CString DataStr;
> DataStr.Format( "%s",  Datachar );

> assumes only that the Datachar array
> contains a valid zero-terminated string.
> If it doesn't then there's not much point
> in copying it into a CString....

> -------------------------------------------------------------------
> "If you think you know the answer,
> then you don't understand the question !"
> -------------------------------------------------------------------



> > Hi,

> > Strugglin' late on a friday with drinkin afoot, does anyone know a
neater
> > faster cheeper way to do this in MSVC++6

> >    CString DataStr;
> >    unsigned char Datachar[100];
> >    int i;
> >    for (i=0;i!=100;i++){
> >           Datachar[i]=DataStr[i];
> >    }

> > Cheers
> > Wal



Tue, 30 Jul 2002 03:00:00 GMT  
 Ok Can anyone get around this...
: Hi,

: Strugglin' late on a friday with drinkin afoot, does anyone know a neater
: faster cheeper way to do this in MSVC++6

:    CString DataStr;
:    unsigned char Datachar[100];

Shouldn't this just be an array of char?

:    int i;
:    for (i=0;i!=100;i++){
:           Datachar[i]=DataStr[i];
:    }

As long as you're not using Unicode strings, surely
    strncpy(DataChar, DataStr, 100);
will do.

--
Any opinions expressed are my own and not necessarily those of Laser-Scan.



Tue, 30 Jul 2002 03:00:00 GMT  
 Ok Can anyone get around this...

Quote:
> Strugglin' late on a friday with drinkin afoot, does anyone know a neater
> faster cheeper way to do this in MSVC++6

Depends on how you want to use it.

If you only need a const string (for use as a source that isn't modified)
then you can simply use the CString directly in most cases without any cast.

If you need to modify the string, then simply do this:

char *p = DataStr.GetBuffer(100);  // or however large you need minimally
...  // do somthing with p
DataStr.ReleaseBuffer();

If you absolutely need to store the data in this char array, then simply use
strcpy.

strcpy(DataChar, DataStr);

But make sure to verify your length and size of buffer to avoid overflows.

Quote:
>    CString DataStr;
>    unsigned char Datachar[100];
>    int i;
>    for (i=0;i!=100;i++){
>           Datachar[i]=DataStr[i];
>    }



Tue, 30 Jul 2002 03:00:00 GMT  
 Ok Can anyone get around this...
Ok, so It's getting late...  <sheepish grin>

I see Ben and Erik have answered it....


Quote:
> He wanted to copy FROM a CString TO an unsigned char[].

> NeilB



> > Neater (but probably not faster),

> > unsigned char Datachar[100];
> > CString DataStr;
> > DataStr.Format( "%s",  Datachar );

> > assumes only that the Datachar array
> > contains a valid zero-terminated string.
> > If it doesn't then there's not much point
> > in copying it into a CString....

> > -------------------------------------------------------------------
> > "If you think you know the answer,
> > then you don't understand the question !"
> > -------------------------------------------------------------------



> > > Hi,

> > > Strugglin' late on a friday with drinkin afoot, does anyone know a
> neater
> > > faster cheeper way to do this in MSVC++6

> > >    CString DataStr;
> > >    unsigned char Datachar[100];
> > >    int i;
> > >    for (i=0;i!=100;i++){
> > >           Datachar[i]=DataStr[i];
> > >    }

> > > Cheers
> > > Wal



Wed, 31 Jul 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Work-around for gets() ok?

2. Anyone have a stray version.rc sitting around?

3. Getting around MULTIPLY DEFINED SYMBOLS in the linker...

4. getting around redirection

5. PRB: Getting E_NOINTERFACE when using DCOM, everything is OK when used locally

6. Determining what canned preprocessor symbols are available

7. Beginner [Q] Using Canned File Open/Save dialog

8. Help with compiling a "canned" program

9. Canned Dialogs

10. To Nudds: OK, OK, C is not portable

11. Ok, ok, you can stop now...

12. Has anyone gotten DELAYLOAD to work?

 

 
Powered by phpBB® Forum Software