473,788 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Length Issue in Char while type casting

Dear All

Please check the below code:

UINT8 MsgLength = 0;

MsgLength = strlen((char *)msg);

if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ? As if now I can say that when the message length is more then
256 then it will
fail, apart form this, do any compiler may fail ? looking forward for
the disscussion on this.

Thanks In Advance
Ranjeet Gupta

Jul 17 '06 #1
4 2115
ra***********@g mail.com writes:
Dear All

Please check the below code:

UINT8 MsgLength = 0;
UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int, although I may be wrong. Your type will probably handle
numbers between 0 and 255.
>
MsgLength = strlen((char *)msg);
strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.

strlen requires a const char *. If you need to cast msg then there's
something probably wrong with your argument.
>
if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ?
Who knows, depends on how msg is declared.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 17 '06 #2


ra***********@g mail.com wrote On 07/17/06 11:45,:
Dear All

Please check the below code:

UINT8 MsgLength = 0;

MsgLength = strlen((char *)msg);

if ( MsgLength == 0 || MsgLength 64) {

//Flow A
} else {

//Flow B
}
do you find any issue with the typecasting of the message, will it
create any serious
problem ?
I don't think it could create a problem, but it
could hide a problem. For instance, if msg is an int
then the compiler must complain about strlen(msg) but
need not complain about strlen((char*)m sg).

In short, the cast is almost certainly useless.
As if now I can say that when the message length is more then
256 then it will
fail, apart form this, do any compiler may fail ? looking forward for
the disscussion on this.
If UINT8 in an unsigned eight-bit type, the code
will misbehave for messages longer than 255 characters,
not 256. Why not use size_t? Or if all you care about
is the branching and you don't really need the length
stored separately, you could use

if (strlen(msg) - 1 64 - 1)

.... relying on the "wrap around" properties of unsigned
integer arithmetic and on the fact that (size_t)-1 is
larger than 63.

--
Er*********@sun .com

Jul 17 '06 #3
In article <b1************ *@ukato.freeshe ll.org>,
Nelu <sp*******@gmai l.comwrote:
>ra***********@ gmail.com writes:
> UINT8 MsgLength = 0;
>UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int,
> MsgLength = strlen((char *)msg);
>strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.
Earlier, though, you assumed that UINT8 was an unsigned int (of some
particular size.) That being the case, for any particular value
integral returned by strlen, the conversion to UINT8 is well defined,
rater than the result of the conversion being "unknown".

"When a value with integral type is demoted to an unsigned
integer with smaller size, the result is the nonnegative remainder
on division by the number one greater than the largest unsigned
number that can be represented in the type with smaller size."
It happens that in a 2s complement system that this corresponds to
throwing away the upper bits and keeping only the bits that fit within
the smaller unsigned integer, but this value-based definition indicates
the value result for other representation systems as well.

In the example you gave, of 3000, then if UINT8 is 8 bits, the result
would be well defined as being 184 (3000 = 256 * 11 + 184).
Now, of course, if the strlen of msg happened to come out as 2816,
the result of the conversion would be 0 (2816 = 256 * 11), so this
property that the conversion result is well defined does not happen
to be a -useful- property for this code snippet...
--
Prototypes are supertypes of their clones. -- maplesoft
Jul 17 '06 #4
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <b1************ *@ukato.freeshe ll.org>,
Nelu <sp*******@gmai l.comwrote:
ra***********@g mail.com writes:
UINT8 MsgLength = 0;
UINT8 does not exist in standard C. I will assume it is an 8 bit
unsigned int,
MsgLength = strlen((char *)msg);
strlen returns a size_t. In your case it will be converted to
UINT8, which may not be able to hold the number. If your string has
3000 characters size_t can hold it but UINT8 will not and the value of
the conversion is unknown.

Earlier, though, you assumed that UINT8 was an unsigned int (of some
particular size.) That being the case, for any particular value
integral returned by strlen, the conversion to UINT8 is well defined,
rater than the result of the conversion being "unknown".

"When a value with integral type is demoted to an unsigned
integer with smaller size, the result is the nonnegative remainder
on division by the number one greater than the largest unsigned
number that can be represented in the type with smaller size."
It happens that in a 2s complement system that this corresponds to
throwing away the upper bits and keeping only the bits that fit within
the smaller unsigned integer, but this value-based definition indicates
the value result for other representation systems as well.

In the example you gave, of 3000, then if UINT8 is 8 bits, the result
would be well defined as being 184 (3000 = 256 * 11 + 184).
Now, of course, if the strlen of msg happened to come out as 2816,
the result of the conversion would be 0 (2816 = 256 * 11), so this
property that the conversion result is well defined does not happen
to be a -useful- property for this code snippet...
I should've said useless, but I didn't diferentiate between the words
when I posted. My mistake. Thank you for pointing that out.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4905
by: srinivas reddy | last post by:
Hi, I have following questions. 1. Does va_arg allow one to read user defined types. My compiler allows but I am wondering whether it is true for all. 2. I wrote the following code. Pardon my omitting declarations etc.. int main() { func(10, "baaaaa");
13
6752
by: Matthew Jakeman | last post by:
If i have a pointer, char *. That i have checked is only 1 character long, is just casting it to a char using (char) the best way or is there another way ?
14
2635
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard says this about the <ctype.h> functions: "The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the
33
3667
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
3
2504
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the structure as a zero length array. The performance improvement that resulted by avoiding the need to dereference the ptr was substantial, but it has left me with an uncertain feeling. In particular, changing an assignment caused the output to...
26
11760
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
24
2111
by: Francine.Neary | last post by:
I've read that you should always cast the argument you pass to isupper(), isalnum(), etc. to unsigned char, even though their signature is int is...(int). This confuses me, for the following reason. The is...() functions can either accept a character, or EOF. But now suppose (as is common) that EOF==(int) -1. Then (unsigned char) EOF will be 255, which is a valid character value! So this casting destroys the possibility to pass EOF to...
19
33440
by: Dancefire | last post by:
Hi, everyone It might be a simple question, but I really don't know the answer. char c = '1'; cout << c; The above code will only output a '1' rather than 0x31; If I use int cast, it can show the number:
13
2735
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.