473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

strange output when formatting char

Hi I am just experimenting a little with typecasting and string formatting. Given the small program using MFC and compiled in Visual Studio Express.

[code]

short int i = 128;
char c;
CString str;
int len;

c = i; //move first byte of int to char.
str.Format("Value of char is %#X", c);

CClientDC dc(this);
len = str.GetLength();
dc.TextOutA(10,10,str,len);

[code]

when i = 127 the output is (as expected) 0X7F. However when i = 128 the output is 0XFFFFFF80. Where are all these F's coming from? Does it have anything to do with the CString class

Suddenly it appears that c is a 4 byte value. I understand that something (strange) should happen since char should only hold -128 to 127. But I would rather just expect 0X80 (binary 1000 0000). Which is what you get when i = -128.

Kind regards

Magnus
May 7 '10 #1
5 2068
weaknessforcats
9,208 Expert Mod 8TB
Those F's are the result of having a negative value. Negative integer values are usually stored in 2's complement format.

Here is 5 in binary:

0101

Let's assume the lead 0 is the sign bit.

To get the 2's complement you a) reverse all bit values and b) add 1:

0000 0101 -> 5
0000 1010 -> reversing bit values
0000 0001 -> the 1 to add
1111 1011 -> the 2's complement. This is -5

Notice the sign bit is turned on. In hex this where your F comes in. The above is 0xFB.

To get back the original 5 just repeat the process:

1111 1011 -> -5
0000 0100 -> reversing bit values
0000 0001 -> the 1 to add
0000 0101 -> the 2's complement. This is +5 back again.
May 7 '10 #2
@weaknessforcats
Thanks for the quick an enlightening answer, but I don't quite understand yet:

int i = 128 would be stored in binary as: 1000 0000. (the first byte in the int, that is)

I assume c = i would give c a binary representation of 1000 0000. Interpreting this in decimal using 2s compliment would give -128. But the hex representation of binary 1000 0000 is 0X80. I still don't see where the F's are coming from (and why there are so many of them)?

Magnus
May 7 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
OK, let's take a 4-byte (32 bits) int with 128:

0000 0000 0000 0000 0000 0000 1000 0000

This is 0x00000080.

To get -128 , follow the rules:

0000 0000 0000 0000 0000 0000 1000 0000 -> 128
1111 1111 1111 1111 1111 1111 0111 1111-> reversing bit values
0000 0000 0000 0000 0000 0000 0000 0001 -> the 1 to add
1111 1111 1111 1111 1111 1111 1000 0000-> the 2's complement. This is -128
Notice the sign bit is turned on. In hex this is 0xFFFFFF80.
May 7 '10 #4
donbock
2,426 Expert 2GB
The type char is signed for some C compilers and unsigned for others -- this is explicitly labeled as implementation-dependent behavior. It appears from your result that char is signed for your compiler.

Let's assume that your platform uses two's-complement encoding for signed integers. Let's further assume that char is 8 bits, short is 16 bits, and int is 32 bits. These are reasonable assumptions.

The value 128 is too big to fit in a char ... that value overflows the char. What ends up in the char after the overflow is undefined behavior; however, it is not uncommon for the upper bits that don't fit to be thrown away. That is apparently what happened in your program, but you cannot rely on that to happen all the time.

Because of the way your program handles overflow, the char contains -128. Suppose we assign the char to an int variable. There is no overflow (-128 easily fits in an int). The int also contains -128.

When you pass arguments to a function those arguments are implicitly cast to the argument types as declared in the function prototype. If there is no function prototype (and also for arguments that map to an ellipsis in the prototype) then the default promotions occur. The default promotion for char and short argument values is to cast to int.

The function prototype for printf contains an ellipsis (see below). Therefore the default promotions occur for all arguments except the first.
Expand|Select|Wrap|Line Numbers
  1. int printf(const char *fmt, ...)
Your printf line passes the char as an argument. It is as if you assigned the char to an int. As a result, printf sees -128 in a 32-bit variable. A 32-bit value is expressed in 8 hexadecimal digits. That's why you see so many F's.
May 7 '10 #5
Wow! Thanks a lot to both donbock and weaknessforcats. That sums it up. First and overflow when putting 128 into unsigned char making it -128. Then a promotion of -128 in the function to an int. Accounting for all the F's and the4-byte appearance.

Magnus
May 7 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Stijn Goris | last post by:
hi all, I have this strange error: when I need to use the database on a page I include connectionDB.php with inculde_once("connectionDB.php"); The connectionDB.php looks like this: <? $db =...
0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
1
by: blix | last post by:
When I output my char to a stream and it has a value of 11, it writes a ^B (vertical tab). This is understandable. When I attempt to stream it back into a char variable, it skips it because a tab...
9
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using the iSeries Client Access Driver ver...
2
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
1
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? ...
0
by: Helge Lenuweit | last post by:
Hello, I am trying to put together an application that (1) shows Console.WriteLine() output when launched from within a console window (that is, from the command prompt) but that (2) **does not...
2
by: rkk | last post by:
Hi, My mergesort program is below. There is a small piece of logical error/bug in this code which I can't figure out, as a result the output array isn't completely sorted. Requesting your help...
2
by: Curtis | last post by:
Hello everyone: I have come to love the ease of updating PHP, since getting used to using it these past few years. Recently, however, when I upgraded from PHP 5.1 to PHP 5.2.0 and again when...
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.