troubleshooting Question

handling input and output of a 16 char hex integer in C++

Avatar of Gene Klamerus
Gene KlamerusFlag for United States of America asked on
C++
2 Comments1 Solution10 ViewsLast Modified:
Okay, so I have to process a very large data file.  This file contains 16 character long hex numbers (such as 090027248000289b).

I need to read these into a program to compare with a separate list of similar values (looking for those which are missing from the list.  This will require a lot of comparison checks.

Rather than comparing with strcmp() or strncmp() it would be far quicker to compare the unsigned long values directly.  This reduces all the steps present in either of these.

The plan was to read in the list of values we do have into an array of unsigned long values and then process the other file against this.

Before putting in all the logic I've just created a quick loop to read the file in and print the values out and I'm having issues.  This is where I need help.  I'm not familiar with converting back and forth between strings representing unsigned longs and actual unsigned longs.

I'm using an fgets to read each line into a buffer (and replacing the \n with a \0 to terminate).  I'm printing out this buffer first to confirm it's good so far.

Then I'm trying an sscanf from the buffer into an element of unsigned longs and printing that with a printf to confirm it converted into the integer and this is where I've run into the issue.

The declaration for my array is:
   unsigned long   parent_ids[200000];

Of course, there are two statements required to get this output; the sscanf, and the printf.

I'm trying to use:
   sscanf(input_buffer, "%lx", &parent_ids[num_parent_ids]);
for the scan and
   printf("%lx", parent_ids[num_parent_ids]);
for the print.

The problem is that the value output is always just the last 8 hex characters of the original string.  So, for 090027248000289b above what prints out is 8000289b.  In other words, I appear to only be reading the last 8 hex chars or the unsigned long is playing games with me or even the print is the root cause.  In short though, I'm struggling to know exactly which of these is happening and how to do it properly.

I would expect an unsigned long should support numbers of the above format (which is 8 bytes long).

I'm using Visual Studio for this on Windows 10.

Could someone help by pointing out which step is in error or which assumptions are wrong and how to correct?
ASKER CERTIFIED SOLUTION
Gene Klamerus
Technical Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros