The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Putting two char values together
Discuss Putting two char values together in the C Programming forum on Dev Shed. Putting two char values together C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 17th, 2004, 09:45 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: 2 h 40 m 45 sec
Reputation Power: 10
|
|
|
Putting two char values together
I want to know how to put two char values together....
The values are read in as chars from a file with letters and numbers..
I use isdigit (ch) to detect a number.... but now I want to put them togther ot get the original number.
I.E: values of file MUNGFHG85NMNN
I read in each character one at a time, but I want to keep the 85 as a number by itself, not 8 5.. so after the program read 8 and then 5 it should put them togther to get 85... any help?
|

February 17th, 2004, 09:55 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
depends. do you want to keep them as a string or convert to an integer
Code:
char value[3];
value[0] = '8';
value[1] = '3];
value [2] = 0;
// as an integer
int value = 0;
value = (value * 10) + '8' - '0';
value = (value * 10) + '3' - '0';
|

February 17th, 2004, 09:55 PM
|
|
Brony & F/OSS Advocate
|
|
Join Date: Jul 2003
Location: Anaheim, CA (USA)
|
|
|
char ch1 = '8';
char ch2 = '5';
int eightyFive = atoi( ch1 ) * 10 + atoi( ch2 );
|

February 17th, 2004, 09:56 PM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
|
atoi() function works only with strings. you are passing a single character.
|

February 18th, 2004, 10:20 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by php4geek char ch1 = '8';
char ch2 = '5';
int eightyFive = atoi( ch1 ) * 10 + atoi( ch2 ); |
As Ancient Dragon said, atoi() works with strings, not with individual characters.
Your code should read:
PHP Code:
char ch1 = '8';
char ch2 = '5';
int eightyFive = (ch1 - '0') * 10 + (ch2 - '0');
Or, in order to use atoi():
PHP Code:
char ch1 = '8';
char ch2 = '5';
char sNum[3];
sNum[0] = ch1;
sNum[1] = ch2;
sNum[2] = '\0'; /* null-terminate the string */
int eightyFive = atoi(sNum);
|

February 19th, 2004, 12:00 AM
|
|
|
can try this also.
Code:
#include <stdio.h>
int main()
{
int num;
char c1='8',c2='5';
num=(int)(c1-'0')*10+(int)(c2-'0');
printf("\n ch is %d",num);
return 0;
}
|

February 19th, 2004, 01:18 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 136
  
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
|
|
More generally, if you have a string of characters and you want to find a sequence of digits in it and turn it into a number you can try something like this. It should be noted that this quick hack will only find the _last_ number embeded in the string; also, for some reason I am getting an undefined reference to function pow() and I am to lazy to figure out what's going wrong and actually test this little bugger; it is after 2 in the morning after all. It should work when the number is at either the begining, end, or in the middle of the string, though ...
Code:
#include <string.h>
#include <stdio.h>
#include <math.h>
int main ( int argc, char **argv )
{
char *str = "abcdefg123hijkl";
int i = strlen ( str ), j=0;
int total = 0;
// step backwards through the string looking for the last digit
while ( i > 0 && !isdigit ( str[--i] ) ) ;
// multiply each digit by successive powers of ten
// until we run out of digits
while ( i >= 0 && isdigit ( str[i] ) )
total += (str[i--] - '0') * pow(10, j++);
printf ( "%i\n", total );
return 0;
}
__________________
- The Immortal ME
There are some who call me ... TIM?
|

February 19th, 2004, 01:46 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
The Immortal ME,
the undefined reference to pow might be happening because you didn't explicitly link the math library. Some compilers, including gcc, force you to do it. Next time you try to compile it, link the math library explicitly. For example, if you use gcc, you would say
gcc -lm program.c
|

February 19th, 2004, 02:01 AM
|
|
Contributing User
|
|
Join Date: Jan 2004
Posts: 136
  
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
|
|
|
I figured that was probably what the problem was, but I don't remember having to link the math library before (I may not have used it recently, though) and I didn't feel like looking up what the heck it would called ...
And I just finished cleaning up my scratch program, too ...
|

February 19th, 2004, 09:03 AM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
no pow() necessary:
moving from left to right with the digits...
Code:
numValue = 0;
loop while successive digits
numValue *= 10;
numValue += value_extracted_from_char;
end loop
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|