Solved

help converting integers to strings and concatenating in C

Posted on 2012-03-16
11
353 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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
 

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countX 22 112
groupSumClump challenge 9 115
word0 challenge 4 68
How to copy documents folder to network folder automatically as backup? 15 86
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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