C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 17th, 2004, 09:45 PM
jahboo jahboo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 jahboo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old February 17th, 2004, 09:55 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
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';

Reply With Quote
  #3  
Old February 17th, 2004, 09:55 PM
codergeek42 codergeek42 is offline
Brony & F/OSS Advocate
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Jul 2003
Location: Anaheim, CA (USA)
Posts: 6,638 codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)codergeek42 User rank is General 22nd Grade (Above 100000 Reputation Level)  Folding Points: 39542 Folding Title: Starter FolderFolding Points: 39542 Folding Title: Starter Folder
Time spent in forums: 1 Month 2 Weeks 6 Days 1 h 33 m 45 sec
Reputation Power: 2474
Send a message via ICQ to codergeek42 Send a message via AIM to codergeek42 Send a message via Yahoo to codergeek42 Send a message via Google Talk to codergeek42
Facebook
char ch1 = '8';
char ch2 = '5';
int eightyFive = atoi( ch1 ) * 10 + atoi( ch2 );
__________________
~~ Peter ~~ :: ( Who am I? ) :: ( Peter's Musings: Uploading myself, bit by bit... ) :: ( Electronic Frontier Foundation ) :: ( I'm a GNU/Linux addict and Free Software Advocate. ) :: ( How to Ask Questions the Smart Way ) :: ( The Fedora Project, sponsored by Red Hat ) :: ( GNOME: The Free Software Desktop Project ) :: ( GnuPG Public Key ) :: ( About me on the WIki )

Reply With Quote
  #4  
Old February 17th, 2004, 09:56 PM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
atoi() function works only with strings. you are passing a single character.

Reply With Quote
  #5  
Old February 18th, 2004, 10:20 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
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); 

Reply With Quote
  #6  
Old February 19th, 2004, 12:00 AM
sunvsunv sunvsunv is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 485 sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level)sunvsunv User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 5 Days 6 h 14 m 32 sec
Reputation Power: 102
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;
}

Reply With Quote
  #7  
Old February 19th, 2004, 01:18 AM
The Immortal ME The Immortal ME is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 136 The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
Send a message via AIM to The Immortal ME
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?

Reply With Quote
  #8  
Old February 19th, 2004, 01:46 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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

Reply With Quote
  #9  
Old February 19th, 2004, 02:01 AM
The Immortal ME The Immortal ME is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 136 The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level)The Immortal ME User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 5 h 56 sec
Reputation Power: 11
Send a message via AIM to The Immortal ME
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 ...

Reply With Quote
  #10  
Old February 19th, 2004, 09:03 AM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 26 m 26 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Putting two char values together

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap