473,498 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static_cast<unsigned>

i m a beginner in C++. I have some questions:
what is static_cast<unsigned>?
i saw someone use static_cast<unsigned> and static_cast<unsigned char>
together. why?
e.g. static_cast<unsigned>(static_cast<unsigned char>(c))
(c is a char).

Thank you!

Bon

Jul 23 '05 #1
6 13233
Me
> i m a beginner in C++. I have some questions:
what is static_cast<unsigned>?
i saw someone use static_cast<unsigned> and static_cast<unsigned char>
together. why?
e.g. static_cast<unsigned>(static_cast<unsigned char>(c))
(c is a char).


Take an implementation where char ranges from -128 to 127, the cast
from char to unsigned char (on this implementation) converts it to the
range 0 to 255 and then the cast to unsigned int leaves it in that 0 to
255 range. Without the unsigned char cast there, converting the char to
an unsigned integer directly (lets say unsigned int ranges from 0 to
0xFFFF on this implementation) would convert it to either 0 to 127 if c
is positive or 0xFFFF+1-128 to 0xFFFF+1-1 if c is negative (all these
ranges here are inclusive).

So basically, it's because char isn't guaranteed to be signed or
unsigned, so we want to force it to become unsigned.

If the code is doing what I think it's trying to do, it's wrong in
general (but works on the majority of 2s complement computers, which is
why you see it in the wild). The correct way is static_cast<const
unsigned char&>(c) because you want to cast the bits of the character
and not the mathematical value of it.

Jul 23 '05 #2
Thanks! this answers more than i asked for.
I assume the "&" is there to ensure the cast of bits of character than
the mathematics value in static_cast<const unsigned char&>(c) rite?
why need the "const"?

Jul 23 '05 #3
Thanks! this answers more than i asked for.
I assume the "&" is there to ensure the cast of bits of character than
the mathematics value in static_cast<const unsigned char&>(c) rite?
why need the "const"?

Jul 23 '05 #4
Me
> Thanks! this answers more than i asked for.
I assume the "&" is there to ensure the cast of bits of character than
the mathematics value in static_cast<const unsigned char&>(c) rite?
why need the "const"?


static_cast<const unsigned char&>(c) is the same thing (with added
semantics) as *static_cast<const unsigned char*>(&c) so you can see
you're basically intepreting memory a different way. The const is there
for two reasons 1. const safety
(http://www.parashift.com/c++-faq-lit...rrectness.html) and 2.
to allow for binding an r-value to a reference
(http://cpptips.hyperformix.com/cpptips/nconst_tmp_ref3)

Jul 23 '05 #5
Me
> char c;
static_cast<const unsigned char&>(c)


Doh, strike that, that code shouldn't compile (I'm way too used to
VC6's broken compiler). The correct way is to get the unsigned bits is:

*static_cast<const unsigned char*>(static_cast<const void*>(&c)) or
*(const unsigned char*)(const void*)(&c).

Some people just do reinterpret_cast<unsigned char&>(c) or even just
(unsigned char&)(c). The C++ standard doesn't guarantee it works (the C
one does however). Personally, I always cast through void* just to play
it safe.

Jul 23 '05 #6
Thank you very much!

Jul 23 '05 #7

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

Similar topics

9
7233
by: GuineaPig | last post by:
Hello, I'm (very) new to c++ and I'm having trouble understanding why this doesn't work. Here's some testcode: #include <iostream.h> int main() { float test;
11
5112
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of...
2
4748
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
3
2334
by: Nate Barney | last post by:
I have: // base class for Vector and Matrix template <unsigned N,typename Value=float> class NonScalar { public: Value *ptr() { return e; } const Value *ptr() const { return e; }
27
4278
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
232
13062
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
15
2798
by: Grizlyk | last post by:
Hello. Returning to question of manual class type identification, tell me, for ordinary inheritance is C++ garantee that dynamic_cast<Derived*>(Base*) can be implemented similarly to ...
11
2954
by: Gerald I. Evenden | last post by:
Working on a Kubuntu 64bit system "c++ (GCC) 4.0.3". The following simple program extracted from p.497 & 499 of N.M.Josurris' "The C++ Standard Library ... " (file t.cpp): 1 #include <string>...
9
1800
by: jabbah | last post by:
yeah i know that the title is not really a question - sorry - i fail to put my question into one sentence so i put it into a storry: background is i am using two different 'ARRAY's in my code....
0
7125
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,...
0
7002
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...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
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...
0
7379
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...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
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...

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.