?
Solved

Outputting a char as an unsigned int

Posted on 2007-11-28
4
Medium Priority
?
589 Views
Last Modified: 2008-02-01
Hi,

I'm attempting to output every possible char as an unsigned int (see code snippet). But this gives the following:

0
...
126
127
4294967168
4294967169
...
4294967295


But what I'm wanting is:
0
...
126
127
128
129
...
255


Thanks,
Uni
char mut=0;
cout<<(unsigned int)mut<<endl;
for(unsigned int i=0;i<256;i++){
	mut++
	cout<<(unsigned int)mut<<endl;
}

Open in new window

0
Comment
Question by:Unimatrix_001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 2000 total points
ID: 20370652
You must use unsigned char.
char type can contain negative values, which causes problems in your case, when you are converting char to unsigned int.

unsigned char mut=0;
for(unsigned int i=0;i<256;i++){
        cout<<(unsigned int)mut<<endl;
            mut++;
}

Also, I shortened your example little bite, because it contained some  unnecessary parts.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20370663
Thank you... :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372007
You could also do it as short as this :

        char c = 0;
        do {
          std::cout << (unsigned int) ((unsigned char) c) << std::endl;
        } while (++c);

or even :

        char c = 0;
        do {
          printf("%d\n", (unsigned char) c);
        } while (++c);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372030
or %u instead of %d ;)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question