Solved

Outputting a char as an unsigned int

Posted on 2007-11-28
4
583 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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 500 total points
Comment Utility
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
Comment Utility
Thank you... :)
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
or %u instead of %d ;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now