Avatar of frostyourself
frostyourself
 asked on

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);
Programming Languages-OtherC

Avatar of undefined
Last Comment
ozo

8/22/2022 - Mon
ozo

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);
ozo

//or
 char full_value_char[99];
 sprintf(full_value_char, "%d%d",high_digit,low_digit);
ozo

if you use full_value_char[3],
"%1s%1s" should have been "%.1s%.1s"
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
frostyourself

ASKER
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)
{
      switch(val_convert){

            case 0:
                  val_convert = 0xC0;
                          break;
            case 1:
                  val_convert = 0xF9;
                      break;
            case 2:
                  val_convert = 0x24;
                      break;
            case 3:
                  val_convert = 0x30;
                      break;
            case 4:
                  val_convert = 0x19;
                      break;
            case 5:
                  val_convert = 0x12;
                      break;
            case 6:
                  val_convert = 0x03;
                      break;
            case 7:
                  val_convert = 0xF8;
                      break;
            case 8:
                  val_convert = 0x00;
                      break;
            case 9:
                  val_convert = 0x18;
                            break;
                                             }
      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.
ozo

fullvalue = bcd_convert(high_digit)<<8+bcd_convert(low_digit);
printf("%04x\n",fullvalue);
frostyourself

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
frostyourself

ASKER
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!!
ozo

bcd_convert(3)==30
bcd_convert(0)==0xc0
if you don't want that, don't bcd_convert
fullvalue = (high_digit<<8)+low_digit;
frostyourself

ASKER
Thank you!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ozo

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.