- Forum
- Visual C++ & C++ Programming
- C++ (Non Visual C++ Issues)
- printf("%02x")
- If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register or Login before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
Results 1 to 11 of 11 Thread: printf("%02x") - December 28th, 2006, 12:22 AM #1 Cooker
- View Profile
- View Forum Posts
Member Join Date Sep 2003 Posts 280 printf("%02x") Hi, I write the following code: Code: #include <stdio.h> int main() { union Data { int i; double x; char str[4]; }; union Data obj = { 2147483647 }; printf( "0x%08x\n", obj.i ); printf( "0x%02x %02x %02x %02x \n", obj.str[3], obj.str[2], obj.str[1], obj.str[0] ); return 0; } I expect the result as 0x7fffffff 0x7fffffff But I don't know why the result is. Code: 0x7fffffff 0x7f ffffffff ffffffff ffffffff What shall I do? Reply With Quote - December 28th, 2006, 03:31 AM #2 screetch
- View Profile
- View Forum Posts
Member Join Date Dec 2006 Posts 154 Re: printf("%02x") when passing an argument to printf,the argument MUST match the pattern you gave. Here, givin %x as a pattern means that you're feeding printf with unsigned int values that it should print in hexa display. But it's not, it's only a char (one byte long) and the stack doesn't look like printf expects it so it prints...something, but not what you expect. Convert each value to an unsigned int firts : Code: #include <stdio.h> int main() { union Data { int i; double x; char str[4]; }; union Data obj = { 2147483647 }; printf( "0x%08x\n", obj.i ); printf( "0x%02x %02x %02x %02x \n", (unsigned int)obj.str[3], (unsigned int)obj.str[2], (unsigned int)obj.str[1], (unsigned int)obj.str[0] ); return 0; } Reply With Quote - December 28th, 2006, 04:24 AM #3 screetch
- View Profile
- View Forum Posts
Member Join Date Dec 2006 Posts 154 Re: printf("%02x") also, you're converting a char to an unsigned int, for an hexadecimal display you should une unsigned chars Reply With Quote - December 28th, 2006, 04:40 AM #4 NMTop40
- View Profile
- View Forum Posts
Elite Member Power Poster Join Date Oct 2000 Location London, England Posts 4,773 Re: printf("%02x") no the solution is to use unsigned char, or to set your compiler such that char is unsigned. Note that technically the output is "undefined" anyway if you set the int member then read the char member, although I don't know of any compiler that will not give the output yours did. Reply With Quote - December 28th, 2006, 05:35 AM #5 SuperKoko
- View Profile
- View Forum Posts
Elite Member Power Poster Join Date Feb 2005 Location Normandy in France Posts 4,590 Re: printf("%02x") Originally Posted by NMTop40 Note that technically the output is "undefined" anyway if you set the int member then read the char member, although I don't know of any compiler that will not give the output yours did. You forgot about the char to int promotion that happens on variadic parameters! So: Code: printf( "0x%02x %02x %02x %02x \n", obj.str[3], obj.str[2], obj.str[1], obj.str[0] ); /* is equivalent to */ printf( "0x%02x %02x %02x %02x \n", (int)obj.str[3], (int)obj.str[2], (int)obj.str[1], (int)obj.str[0] ); Aliasing int and unsigned int is allowed by the standard, and, if the integer is non-negative, the standard guarantees that value representations of int and unsigned int are the same. In other words, if you read a non-negative int through an unsigned int lvalue, you'll got the correct value. For, negative values, behavior is undefined by omission (in practice, it may be a problem on platforms having hardware trap representations but it won't be a problem on x86 AFAIK). Here, this program, very probably, has negative char values converted to int and read as unsigned int. In order to avoid this problem, a conversion to unsigned char (whose behavior is well defined) can be used: Code: printf( "0x%02x %02x %02x %02x \n", (unsigned char)obj.str[3], (unsigned char)obj.str[2], (unsigned char)obj.str[1], (unsigned char)obj.str[0] ); /* or, if you are paranoid or that MAX_UCHAR>MAX_INT */ printf( "0x%02x %02x %02x %02x \n", (unsigned)(unsigned char)obj.str[3], (unsigned)(unsigned char)obj.str[2], (unsigned)(unsigned char)obj.str[1], (unsigned)(unsigned char)obj.str[0] ); Output is unspecified but will most likely be 0x7f ff ff ff on i386. Alternatively the str member of Data can be defined as an unsigned char array. Cooker: You get this strange output because, on your platform, probably: 1) char is signed and has a two's complement representation. 2) The second, third and fourth byte of your number has all-bits-one and thus, are negative two's complement signed chars with value -1 3) When converthing from char to int, sign is extended and value is preserved (-1 with binary representation FF is converted to -1 with representation FFFFFFFF) 4) The %02x displays "unsigned hex numbers", with minimum (but not maximum) length 2, and thus displays ffffffff Last edited by SuperKoko; December 28th, 2006 at 05:40 AM. "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards. Club of lovers of the C++ typecasts cute syntax: Only recorded member. Out of memory happens! Handle it properly! Say no to g_new()! Reply With Quote - December 28th, 2006, 09:36 AM #6 Cooker
- View Profile
- View Forum Posts
Member Join Date Sep 2003 Posts 280 Re: printf("%02x") Hi,SuperKoko, You really solve my problem. Thanks you very much. Reply With Quote - December 29th, 2006, 12:26 AM #7 Cooker
- View Profile
- View Forum Posts
Member Join Date Sep 2003 Posts 280 Re: printf("%02x") Code: #include <stdio.h> int main() { struct fakeint { short f0; short zeros; }; struct fakeint strange; struct fakeint strange2; strange.f0=-240; strange2.f0=240; strange2.zeros=0; strange.zeros=0; printf("%d %d\n",strange.f0, strange ); printf("%d %d\n",strange2.f0,strange2); return 0; } Copied from http://www.owasp.org/index.php/Sign_extension_error Code: Output: -240 65296 240 240 Why the result "65296" is ? Reply With Quote - December 29th, 2006, 05:00 AM #8 SuperKoko
- View Profile
- View Forum Posts
Elite Member Power Poster Join Date Feb 2005 Location Normandy in France Posts 4,590 Re: printf("%02x") Originally Posted by Cooker Why the result "65296" is ? In short words, f0 has not be "sign extended" inside strange but "zero extended" and consequently, -240 has been interpreted as an unsigned short equal to 65536-240. In longer words: strange.f0 has a representation 10 FF strange.zeros has a representation 00 00 strange has a representation 10 FF 00 00 And will be interpreted as a little endian int... 0000FF10h == FFFFh - F0h == 65536 - 240 == 65296. And, of course this code is 100% non-portable and contain a lot of UB. "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards. Club of lovers of the C++ typecasts cute syntax: Only recorded member. Out of memory happens! Handle it properly! Say no to g_new()! Reply With Quote - December 29th, 2006, 08:09 AM #9 Cooker
- View Profile
- View Forum Posts
Member Join Date Sep 2003 Posts 280 Re: printf("%02x") Originally Posted by SuperKoko In short words, f0 has not be "sign extended" inside strange but "zero extended" and consequently, -240 has been interpreted as an unsigned short equal to 65536-240. In longer words: strange.f0 has a representation 10 FF strange.zeros has a representation 00 00 strange has a representation 10 FF 00 00 And will be interpreted as a little endian int... 0000FF10h == FFFFh - F0h == 65536 - 240 == 65296. And, of course this code is 100% non-portable and contain a lot of UB. How do you know that f0 has not "sign extension" but "zero extension"? How can I know when I use "sign extension" or "zero extension"? For example, Code: short fo = -240; printf( "%d", fo); How do I estimate fo that has "sign extension" or "zero extension"? Reply With Quote - December 29th, 2006, 09:50 AM #10 SuperKoko
- View Profile
- View Forum Posts
Elite Member Power Poster Join Date Feb 2005 Location Normandy in France Posts 4,590 Re: printf("%02x") Originally Posted by Cooker How do you know that f0 has not "sign extension" but "zero extension"? How can I know when I use "sign extension" or "zero extension"? For example, Because I read the code: Code: strange.zeros=0; The code: 1) Puts some bytes of data (10 FF) inside the first half of a 4 bytes structure (with strange.f0=-240} 2) Puts some zeroes inside the upper half of the 4 bytes structure (with strange.zeros=0). So, we can say that we've artficially "extended" a short -240 with zeroes so that it fits in a 4 bytes structure. Code: short fo = -240; printf( "%d", fo); How do I estimate fo that has "sign extension" or "zero extension"? Here, there is sign extension because integer promotion from short to int is value preserving... implying that there is sign extension on two's complement machines. Passing this structure as variadic argument is a different problem: 1) There is no promotion (what would it mean?) on structures. 2) This structure exactly fits in the place of an int... And, it will (though that's very un-portable) be interpreted as the bits of an int. "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards. Club of lovers of the C++ typecasts cute syntax: Only recorded member. Out of memory happens! Handle it properly! Say no to g_new()! Reply With Quote - December 29th, 2006, 10:28 AM #11 Cooker
- View Profile
- View Forum Posts
Member Join Date Sep 2003 Posts 280 Re: printf("%02x") Hi,SuperKoko, Thank you. I got it! Reply With Quote Quick Navigation C++ (Non Visual C++ Issues) Top - Site Areas
- Settings
- Private Messages
- Subscriptions
- Who's Online
- Search Forums
- Forums Home
- Forums
- Visual C++ & C++ Programming
- Visual C++ Programming
- Visual C++ FAQs
- C++ (Non Visual C++ Issues)
- C++ and WinAPI
- Managed C++ and C++/CLI
- Visual C++ Bugs & Fixes
- Xamarin
- Graphics Programming
- Multithreading
- Network Programming
- Driver Development
- C# Programming
- C-Sharp Programming
- Visual Basic Programming
- Visual Basic 6.0 Programming
- Visual Basic .NET
- VBForums
- Windows 8 and Later Store Development
- Modern Windows Apps (Metro)
- Other .NET Programming
- ASP.NET
- .NET Framework
- .NET Installation and Configuration Issues
- ADO.NET
- Windows Presentation Foundation (WPF) & XAML forum
- Java Programming
- Java Programming
- Other Programming
- AJAX
- Scripting - Client Side
- Crystal Reports
- Database
- XML
- Wireless/Mobile Development
- Assembly
- Scripting - Server Side (PHP, Perl, etc.)
- SharePoint
- Python
- Python Articles
- General Discussion
- General Developer Topics
- Project Planning, Design, and Management
- Testers and Testing
- Algorithms & Data Structures
- IoT, IoE, and Maker Forum (on VBForums)
- General Discussion / Chit Chat
- Announcements, Press Releases, & News
- CodeGuru Community
- Feedback
- Articles Suggestions / Requests
- Programming Projects
- Game Engine Project
- C# Game(s) Project
- C++ Coding Project
- Project: Code War
- Testing Area
- Slow Chat Archives
- eCamp Chat: Windows 8 for Developers
- Slow Chat: Talk with Microsoft Developer Teams
- Slow Chat: Developing Multithreaded Applications
- Slow Chat: C++0x
- Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
- Jobs
- Looking for Work
- Open Positions (Jobs)
- CodeGuru Technical FAQs
- C++ FAQs
- STL FAQs
- Windows SDK FAQs
- Visual C++ FAQs
- MFC FAQs
- ATL FAQs
- .NET Framework (non-language specific) FAQs
- C# FAQs
- Visual Basic .NET FAQs
- Visual Basic FAQs
- CodeGuru Individual FAQs
- CodeGuru Individual Visual Basic FAQs
- Retired Forum Areas
- Silverlight
- Directory Services
- General Windows and DNA Programming
- Windows OS Issues
« Previous Thread | Next Thread » Posting Permissions - You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
- BB code is On
- Smilies are On
- [IMG] code is On
- [VIDEO] code is On
- HTML code is Off
Forum Rules -- Blue Codeguru -- Default Mobile Style ---- Child of Default Mobile Style | Click Here to Expand Forum to Full Width Featured * The Best Reasons to Target Windows 8 Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform. - * Porting from Android to Windows 8: The Real Story Do you have an Android application? How hard would it really be to port to Windows 8?
- * Guide to Porting Android Applications to Windows 8 If you've already built for Android, learn what do you really need to know to port your application to Windows Phone 8.
- * HTML5 Development Center Our portal for articles, videos, and news on HTML5, CSS3, and JavaScript
- * Windows App Gallery See the Windows 8.x apps we've spotlighted or submit your own app to the gallery!
|