65.9K
CodeProject is changing. Read more.
Home

How to convert HUGE numbers to Hex string and back

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.28/5 (15 votes)

Feb 5, 2003

viewsIcon

55423

downloadIcon

814

How to convert big numbers to ready-to-display hex numbers.

Introduction

During my work with systems that use a huge memory address range, I had to display memory addresses in hex format that were very hard to convert into string. The usual %x flag will only work with up to 16 bit hex values and any code example you will see on the Internet is limited to even less than that.

So, I sat down and wrote 2 generic functions you can use:

  • hex_string_to_long - This one will take a hex string such as "10FBBC45" and convert it to a very long integer.
  • convert_long_to_hex - Will ask you for the number you want to convert and for a string reference that it will return the answer into.

Don't forget to include the math.h library in your code and you should be all set. Just copy and paste the functions to your code.