help converting integers to strings and concatenating in C

I'm trying to convert two integers into strings, concatenate them, and convert the result back to an integer. I'm not sure the best way to do this. I tried using sprintf but it didn't work and the characters were null when I printed them out.

Here are the relevant parts of the code:

alt_u8 high_digit, low_digit;
char high_digit_char, low_digit_char, full_value_char;

sprintf(high_digit_char, "%d", high_digit);
sprintf(low_digit_char, "%d", low_digit);
sprintf(full_value_char, "%s %s",high_digit_char, low_digit_char);
full_value = atoi(full_value_char);
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

char high_digit_char[2], low_digit_char[2], full_value_char[3];
sprintf(high_digit_char, "%d", high_digit%10);
sprintf(low_digit_char, "%d", low_digit%10);
sprintf(full_value_char, "%1s%1s",high_digit_char, low_digit_char);
full_value = atoi(full_value_char);
 char full_value_char[99];
 sprintf(full_value_char, "%d%d",high_digit,low_digit);
if you use full_value_char[3],
"%1s%1s" should have been "%.1s%.1s"
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

frostyourselfAuthor Commented:
I'm still not getting the correct result, these are the integer and corresponding character values printed out:

high int: 18 low int: 18
full val int: 2c
low char: 4 high char: 4
full char: 44

What is the point of the %10 in high_digit%10 within the sprintf?

Maybe I should post another part of the code:

high_digit = bcd_convert(high_digit);
low_digit = bcd_convert(low_digit);

alt_u8 bcd_convert(alt_u8 val_convert)

            case 0:
                  val_convert = 0xC0;
            case 1:
                  val_convert = 0xF9;
            case 2:
                  val_convert = 0x24;
            case 3:
                  val_convert = 0x30;
            case 4:
                  val_convert = 0x19;
            case 5:
                  val_convert = 0x12;
            case 6:
                  val_convert = 0x03;
            case 7:
                  val_convert = 0xF8;
            case 8:
                  val_convert = 0x00;
            case 9:
                  val_convert = 0x18;
      return val_convert;

The high digit and low digit are converted to these hex values, so for example if high digit is 9 and low digit is 9, I would want "1818" to be the full value.
fullvalue = bcd_convert(high_digit)<<8+bcd_convert(low_digit);
frostyourselfAuthor Commented:
It's still not working, I added to the printf statement to print the high and low digits separately:

printf("high: %x low: %x full: %04x\n",high_digit,low_digit,full_value);

and this is part of the result:

high: 18 low: 12 full: 0000
high: 18 low: 3 full: 0000
high: 18 low: f8 full: 0018
high: 18 low: 0 full: 1800
high: 18 low: 18 full: 0018
//Sorry forgot the parentheses
fullvalue = (bcd_convert(high_digit)<<8)+bcd_convert(low_digit);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
frostyourselfAuthor Commented:
Thank you, there is just one slight problem:

When there is a single digit (0x00 or 0x03), this happens:

high: c0 low: 3 full: c030
high: f9 low: 0 full: f9c0

Do you know how to fix that? Thank you!!
if you don't want that, don't bcd_convert
fullvalue = (high_digit<<8)+low_digit;
frostyourselfAuthor Commented:
Thank you!
What is the point of the %10 in high_digit%10 within the sprintf?
To ensure that %d only prints a single digit.
Your original code tried to put %d into a single character, so high_digit_char[2] allocated space for a single character string, with null terminator.
if %d was permitted to print more than a single digit, it would have overflowed the space available.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.