Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Outputting a char as an unsigned int

Posted on 2007-11-28
4
Medium Priority
?
591 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

636 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