Solved

help converting integers to strings and concatenating in C

Posted on 2012-03-16
11
351 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 84

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 84

Expert Comment

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

Expert Comment

by:ozo
ID: 37731504
if you use full_value_char[3],
"%1s%1s" should have been "%.1s%.1s"
0
 

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 84

Expert Comment

by:ozo
ID: 37731732
fullvalue = bcd_convert(high_digit)<<8+bcd_convert(low_digit);
printf("%04x\n",fullvalue);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 84

Accepted Solution

by:
ozo earned 500 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 84

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 84

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now