89667

C: Correct way to print “unsigned long” in hex

Question:

I have a function that gets an <strong>unsigned long</strong> variable as parameter and I want to print it in Hex.

What is the correct way to do it?

Currently, I use <strong>printf</strong> with <strong>"%lx"</strong>

void printAddress(unsigned long address) { printf("%lx\n", address); }

Should I look for a printf pattern for <strong>unsigned</strong> long hex? (and not just "long hex" as mentioned above)

Or does <strong>printf</strong> convert numbers to hex only using the bits? - so I should not care about the sign anyway?

Answer1:

You're doing it right.

From <a href="http://linux.die.net/man/3/printf" rel="nofollow">the manual page</a>:

<blockquote>

o, u, x, X

The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation.

</blockquote>

So the value for x should always be unsigned. To make it long in size, use:

<blockquote>

l

(ell) A following integer conversion corresponds to a long int or unsigned long int argument [...]

</blockquote>

So %lx is unsigned long. An address (pointer value), however, should be printed with %p and cast to void *.

Answer2:

I think the following format specifier should work give it a try

printf("%#lx\n",address);

Recommend

  • “Installation of the application failed. Run time error has eccured. Fix the capabilities in WMAppMa
  • What is the difference between ui-view in angularjs and ion-nav-view in ionic
  • How to replace elements in one XDocument with elements from another XDocument?
  • lmerTest::anova not showing p-values
  • How to add cropVariants to an image field in TYPO3 flexform
  • laravel 5.1 auth csrf token mismatch
  • Rails traverse deep array of hashes
  • TeamCity: Scripting elements jsp:declaration, jsp:expression, jsp:scriptlet are disallowed here
  • How application get update from apple store?
  • Attach an event handler to a StopWatch
  • Sending PNG attachment via Android GMail app
  • Login working for Iphone but not android
  • failed to resolve: com.google.firebase:firebase-core:16.0.0
  • limit python input to int
  • Docker build error: “could not connect to server” (behind proxy)
  • Delete get variables and .php using htaccess
  • Velocity (VM) template request parameters: Getting GET variables
  • android 3G signal strength
  • Is it possible to add a hyperlink to a UIAlertController?
  • Open a cmd program with full functionality (i/o)
  • Limit number of button clicks
  • A library that can fill Java object with data declared in Excel file
  • Eric5: The OK button of 'new project' dialog is disable
  • jQuery - Prevent activation on first click
  • Getting/building the SQL (with parameters) from NHibernate 3.2
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • Use PHP to Replace HTML with HTML
  • SQL query for game rental website - game delivery report
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • Bundling python(“.py”)files along with java class files for a web application
  • How does the dispatcher work when mixing sync/async with serial/concurrent queue?
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • Debugging php script timeout?
  • Magento-Change Attribute of All Products
  • How to join two tables from different databases
  • Disable account chooser FirebaseUI React
  • Creating 2d platforms using JavaScript
  • Typeahead.js does give me suggestions but doesn't select them
  • jQuery scrollTop if URL has hash
  • Another “Cannot make static reference…” Question