Solved

help converting integers to strings and concatenating in C

Posted on 2012-03-16
11
345 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

19 Experts available now in Live!

Get 1:1 Help Now