Solved

Outputting a char as an unsigned int

Posted on 2007-11-28
4
584 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

895 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

21 Experts available now in Live!

Get 1:1 Help Now