Avatar of Gene Klamerus
Gene Klamerus
Flag for United States of America asked on

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

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?

Avatar of undefined
Last Comment

8/22/2022 - Mon
Gene Klamerus

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Hi Gene,
Glad you figured it out. Were  you able to print out the unsigned long long value. If so, would you mind sharing the printf statement in a post?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck