Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

help converting integers to strings and concatenating in C

Posted on 2012-03-16
11
Medium Priority
?
375 Views
Last Modified: 2012-03-16
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);
0
Comment
Question by:frostyourself
  • 7
  • 4
11 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 37731487
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);
0
 
LVL 85

Expert Comment

by:ozo
ID: 37731495
//or
 char full_value_char[99];
 sprintf(full_value_char, "%d%d",high_digit,low_digit);
0
 
LVL 85

Expert Comment

by:ozo
ID: 37731504
if you use full_value_char[3],
"%1s%1s" should have been "%.1s%.1s"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:frostyourself
ID: 37731565
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.
0
 
LVL 85

Expert Comment

by:ozo
ID: 37731732
fullvalue = bcd_convert(high_digit)<<8+bcd_convert(low_digit);
printf("%04x\n",fullvalue);
0
 

Author Comment

by:frostyourself
ID: 37731766
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
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 37731773
//Sorry forgot the parentheses
fullvalue = (bcd_convert(high_digit)<<8)+bcd_convert(low_digit);
0
 

Author Comment

by:frostyourself
ID: 37731795
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!!
0
 
LVL 85

Expert Comment

by:ozo
ID: 37731815
bcd_convert(3)==30
bcd_convert(0)==0xc0
if you don't want that, don't bcd_convert
fullvalue = (high_digit<<8)+low_digit;
0
 

Author Closing Comment

by:frostyourself
ID: 37731824
Thank you!
0
 
LVL 85

Expert Comment

by:ozo
ID: 37731908
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question