How to retrieve the length from unsigned char data type?  
Author Message
highstrike





PostPosted: Tue Aug 22 05:08:41 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type?

const unsigned char c_strData[] = "JunkData";

how can i find the length of c_strData at runtime?????

Visual Studio62  
 
 
Heinz





PostPosted: Tue Aug 22 05:08:41 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type?

> const unsigned char c_strData[] = "JunkData";
>
> how can i find the length of c_strData at runtime?????

Did you try strlen, perhaps with a cast to char const*?

Heinz

 
 
Alamelu





PostPosted: Tue Aug 22 07:16:01 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? const unsigned char strData[] = "Junk";
int nLen = strlen(static_cast<const unsigned char*>(strData));

When doing so.... it is giving below error

error C2664: 'strlen' : cannot convert parameter 1 from 'const unsigned char
*' to 'const char *'






> > const unsigned char c_strData[] = "JunkData";
> >
> > how can i find the length of c_strData at runtime?????
>
> Did you try strlen, perhaps with a cast to char const*?
>
> Heinz
>
>
 
 
David





PostPosted: Tue Aug 22 07:34:50 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type?

> const unsigned char strData[] = "Junk";
> int nLen = strlen(static_cast<const unsigned char*>(strData));
>
> When doing so.... it is giving below error
>
> error C2664: 'strlen' : cannot convert parameter 1 from 'const unsigned char
> *' to 'const char *'
>

Alamelu:

Try

const unsigned char strData[] = "Junk";
int nLen = strlen(static_cast<const char*>(strData));

But why are you using unsigned char in the first place? Why not just

const char strData[] = "Junk";
int nLen = strlen(strData);

or

const char* strData = "Junk";
int nLen = strlen(strData);

? Doesn't this work?

David Wilkinson
 
 
Ajay





PostPosted: Tue Aug 22 08:09:29 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? > const unsigned char c_strData[] = "JunkData";
>
> how can i find the length of c_strData at runtime?????

strlen has already been recommended. Since this is a MFC ng, why not
use CString?

---
Ajay

 
 
Alamelu





PostPosted: Tue Aug 22 08:58:02 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? > Try
>
> const unsigned char strData[] = "Junk";
> int nLen = strlen(static_cast<const char*>(strData));

doesnt work....

> But why are you using unsigned char in the first place? Why not just

There are reasons for me to use unsigned char
1) I am passing this data to a BYTE data type..
2) If i am definiing it as char and getting the length using "strlen" when
it is unicode, only half of the data (For eg: J_u_) is getting passed to the
function
3) This data i am going to write to a buffer which is stored as BYTE

so i thought unsigned char must suffice.....

but i am not able to retrieve its length.......




>
> > const unsigned char strData[] = "Junk";
> > int nLen = strlen(static_cast<const unsigned char*>(strData));
> >
> > When doing so.... it is giving below error
> >
> > error C2664: 'strlen' : cannot convert parameter 1 from 'const unsigned char
> > *' to 'const char *'
> >
>
> Alamelu:
>
> Try
>
> const unsigned char strData[] = "Junk";
> int nLen = strlen(static_cast<const char*>(strData));
>
> But why are you using unsigned char in the first place? Why not just
>
> const char strData[] = "Junk";
> int nLen = strlen(strData);
>
> or
>
> const char* strData = "Junk";
> int nLen = strlen(strData);
>
> ? Doesn't this work?
>
> David Wilkinson
>
 
 
Tom





PostPosted: Tue Aug 22 09:01:26 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? How about something like:

CString cs = c_strData;

cs.GetLength();

Tom



> const unsigned char c_strData[] = "JunkData";
>
> how can i find the length of c_strData at runtime?????


 
 
Joseph





PostPosted: Tue Aug 22 09:00:12 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? Of course, this raises several questions, such as why you have chosen to represent a
string using unsigned char, why you are using 'char' at all, and whether or not your
unsigned char data will only represent text strings.

The "natural" representation of a string in C/C++ is as a character data type, so why did
you choose an unsigned char datatype?

It is good programming practice to assume that the 'char' datatype, signed or unsigned,
does not exist as a means of representing character strings, except in rare and ****
circumstances. You have not indicated that this is such a circumstance. You would have
been much better off writing
const CString c_strData(_T("JunkData"));
for example. Unless there is compelling reasons to assume 8-bit-only characters, the
next-best choice would be
const TCHAR c_strData[] = _T("JunkData");
which is Unicode-compliant.

If you only plan to use normal character strings, the way you get the length is the way
you *always* get the length of a character string. For your restricted example of
'unsigned char', you would write the usual
int len = strlen(c_strData);
which, if you've done your job right and enabled level-4 diagnostics in the compiler, will
complain (rightly) about the unsigned/signed character mismatch, so you will need to cast
the argument as (char) to get it to compile, which goes back to the question of why you
chose unsigned char as a representation for a string; for proper programming practice in
the general case, you would have written
int len = _tcslen(c_strData);
(it's either _tcslen or tcslen, check tchar.h, the use of _ is inconsistent). This gives
the length in characters (see below).

Given it is a const array, you could also have done
sizeof(c_strData);
but that's a bad habit to get into, because you have not said what "length" you want: the
length in bytes, or the length in characters, which are different values. The length in
bytes is the length in characters only in the restricted case where you are committed, for
external reasons, to 8-bit characters. Since this is only done in the aforementioned rare
and **** cases, the true length in bytes is either
sizeof(c_strData) - 1;
or
_tcslen(c_strData) * sizeof(TCHAR);
while the length in characters is either
(sizeof(c_strData) / sizeof(TCHAR)) - 1;
or
_tcslen(c_strData);

Note that sizeof() a string includes the terminal NUL character.

If your unsigned char represents binary data (not necessarily strings), then sizeof() will
give the true length in bytes in this case. If the data were computed at runtime and
therefore non-const, and could contain embedded 0 bytes, then sizeof() will not work and
you will have to maintain the length separately. By the way, instead of writing out
'unsigned char', it is more common to use the Windows datatype BYTE
const BYTE c_strData[] = { 'A', 0, 'B', 0, 'C', 0xFF, 0xFE, 23, 0, 14};
In this case, there is no implicit NUL added because of the string literal and the
embedded 0 bytes mean that strlen would give an erroneous result (1).
joe



>const unsigned char c_strData[] = "JunkData";
>
>how can i find the length of c_strData at runtime?????
Joseph M. Newcomer [MVP]

Web: http://www.hide-link.com/
MVP Tips: http://www.hide-link.com/
 
 
Mihai





PostPosted: Wed Aug 23 01:20:15 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? > const unsigned char c_strData[] = "JunkData";
>
> how can i find the length of c_strData at runtime?????

If it really is a string, then use char, and strlen will do.

unsigned char is for random data (and you can use BYTE to make it even more
clear). And then you have no way to retrieve the length.


--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
 
 
Mihai





PostPosted: Wed Aug 23 01:24:05 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type? > const unsigned char c_strData[] = "JunkData";
>
> how can i find the length of c_strData at runtime?????

I did not read this carefuly.
If you are in the same scope with c_strData and the c_strData
is defined as you show, you can use sizeof(c_strData)
(and will include the ending zero)

But if you pass it to a function and what you see is really
void function( const unsigned char * c_strData ) {
...
}
then you are out of luck.


--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
 
 
David





PostPosted: Wed Aug 23 05:54:37 CDT 2006 Top

MFC >> How to retrieve the length from unsigned char data type?

>>Try
>>
>>const unsigned char strData[] = "Junk";
>>int nLen = strlen(static_cast<const char*>(strData));
>
>
> doesnt work....
>
>
>>But why are you using unsigned char in the first place? Why not just
>
>
> There are reasons for me to use unsigned char
> 1) I am passing this data to a BYTE data type..
> 2) If i am definiing it as char and getting the length using "strlen" when
> it is unicode, only half of the data (For eg: J_u_) is getting passed to the
> function
> 3) This data i am going to write to a buffer which is stored as BYTE
>
> so i thought unsigned char must suffice.....
>
> but i am not able to retrieve its length.......
>
[snip]

Alamelu:

Yes, sorry, you have to use reinterpret_cast (or C cast):

const unsigned char strData[] = "Junk";
int nLen = strlen(reinterpret_cast<const char*>(strData));

But if your data is a string I think tou should define it as such, and
only convert to BYTE* when you ned to. Also you should not be using
strlen with "Unicode" data. How about

const TCHAR strData[] = _T("Junk");
int nLen = _tcslen(strData)*sizeof(TCHAR);
BYTE* pByte = reinterpret_cast<BYTE*>(strData);

David Wilkinson