Thread: How would you do this conversion?

  1. #1
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37

    How would you do this conversion?

    Say you have the argument to main in the form of ./program 447 758. How would the separation of those two numbers into integers be done for example to gain the result of.

    Code:
    first_num[0] = 4;
    first_num[1] = 4;
    first _num[2] = 7;
    
    second_num[0] = 7;
    second_num[1] = 5;
    second_num[2] = 8;
    Its driving me mad, i know its simple but can't seem to grasp it. Actual code would be extremely appreciated. Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    For starters try to come up with an algorithm for doing this task, coding it will become a lot simpler afterwards.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    those argurment are string already

    argv[1][0] will be 4 argv[1][2] will be 7 and so on
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    those argurment are string already

    argv[1][0] will be 4 argv[1][2] will be 7 and so on
    actually it will be '4', '7' etc

    to convert these chars to int -
    argv[1][0] - '0' should be used
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by vart View Post
    actually it will be '4', '7' etc
    que?
    to convert these chars to int -
    argv[1][0] - '0' should be used
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The numeric value of each character will be the '4' and '7'. To make that into an integer, one needs to subtract the value of '0'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    well, thats what i said in my first post ...
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    well, thats what i said in my first post ...
    not even close
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    just because i did not put ' ' does not mean that i imply that its already an integer, and you putting it does not imply it to be a character. i did mention that it is a "string"...which is an array of characters.
    Last edited by creeping death; 04-04-2009 at 01:45 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  10. #10
    Registered User redruby147's Avatar
    Join Date
    Sep 2008
    Location
    England
    Posts
    37
    Solved, i stumbled upon this

    C: How can I split a number into digits? - Yahoo! Answers

    and then did this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 5
    
    int main (int argc, char *argv[])
    {
    	int val;
    	int whole1; /*stores the whole number*/
    	int seperated_whole1[MAX];/*seperate digits of whole1*/
    	int i = MAX - 1;/*for use in the seperation loop*/
    
    	whole1 =  atoi(argv[1]);
    	/*
    	*This loop splits the whole1 integer into seperate digits for the array in preperation for
    	*individual manipulation
            */
    	
            while (whole1 > 0)
    	{
    		seperated_whole1[--i] = whole1 % 10;
    		whole1 /= 10;
    	}
    
    
    	printf("%d %d %d %d\n", seperated_whole1[0], seperated_whole1[1], seperated_whole1[2], seperated_whole1[3]);
    	return 0;
    }
    I hope someone who has the same problem comes across this

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    just because i did not put ' ' does not mean that i imply that its already an integer, and you putting it does not imply it to be a character. i did mention that it is a "string"...which is an array of characters.

    yes, yes... and that you come to C programming does not imply that you are talking about C language...


    bla-bla-bla...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by vart View Post
    yes, yes... and that you come to C programming does not imply that you are talking about C language...


    bla-bla-bla...

    yeah , talking *about* C language NOT talking *in* C language.
    Last edited by creeping death; 04-04-2009 at 02:43 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM