Convert

This is a discussion on Convert within the C++ Programming forums, part of the General Programming Boards category; I am trying to convert a char array into an int array, does anyone know how to start setting up ...

  1. #1
    Jae
    Jae is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Convert

    I am trying to convert a char array into an int array, does anyone know how to start setting up the loop?

    Jae

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    depends on the char array. post an example of the array, such as
    Code:
    char array[] = "12345";
    If you want to convert each digit in that array to an integer, then the most portable way will be to put it into another null-terminated string array and use atoi() to convert it.
    Code:
    char array[] = "12345";
    char buf[2] = {0}
    buf[0] = array[0];
    int n = atoi(buf);
    simply subtracting '0' from the digit might work on many operating sytems, but there is no guarantee that the digits have consecutive ascii values. for example, the following code is not portable.
    Code:
    char array[] = "12345";
    int n = array[0] - '0';

  3. #3
    Jae
    Jae is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Thanks

    Thanks Ancient Dragon, that solved my problem.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Ancient Dragon
    ...but there is no guarantee that the digits have consecutive ascii values. for example, the following code is not portable.
    yes there is. the ASCII chart is what it is. If your system uses ASCII, you're guaranteed that 65 = 'A', 66 = 'B', and 0x30-0x39 are 1-9. so long as your system uses ASCII, '0' will always be equal to (char)48

    and atoi() is less portable - it's not even standard code.

    as for the OP's question, you can just walk through your array, and cast each one. for example, the most portable way, is in fact, to rely on the ASCII table:
    Code:
    #include<iostream>	//for console I/O
    #include<cstring>	//for strlen
    
    int main()	//main program entrypoint
    {
    	char letters[]="12345";			//a character array
    	const int ARRLEN=strlen(letters);	//the length of the array
    	int*numbers=new int[ARRLEN];		//an int array the same length
    	
    	for(int i=0;i<ARRLEN;i++)		//loop through the array
    	{
    		numbers[i]=static_cast<int>(letters[i]-'0');	//make a cast
    		std::cout<<numbers[i]<<", ";	//output the results
    	}
    
    	std::cout<<std::endl;			//newline and buffer flush
    	return 0;				//return to the OS
    }
    In that example, I used a variable size on the integer array because that frees you up to change the size of the character array and not worry about breaking your code.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by major_small
    yes there is. the ASCII chart is what it is. If your system uses ASCII,.
    you just proved my point -- not every system uses ASCII formats, which means numeric digits may (or may not) be consecutive.

    Quote Originally Posted by major_small
    and atoi() is less portable - it's not even standard code
    Yes it is -- see this
    Last edited by Ancient Dragon; 10-19-2005 at 11:31 AM.

  6. #6
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by major_small
    and atoi() is less portable - it's not even standard code.
    I think it is. Perhaps you meant itoa()?

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, but it's a whole lot more portable than using atoi(). whether you're using ascii or not is system dependant, whereas whether you can use atoi() is compiler-dependant. and you can always test the system to see what character coding it's using, and adjust during runtime to suit the system. for example, you could have a simple loop that casts the letters '0'..'9' to integers. if the integers are consecutive, then you can use that method. If not, you can have your program create a lookup table, or find some other method.

    edit: in writing that, I realized what I wrote before wasn't the most portable way... I think this is:
    Code:
    #include<iostream>	//for console I/O
    #include<cstring>	//for strlen
    
    int findint(const char letter);
    
    int main()	//main program entrypoint
    {
    	char letters[]="12345";			//a character array
    	const int ARRLEN=strlen(letters);	//the length of the array
    	int*numbers=new int[ARRLEN];		//an int array the same length
    	
    	for(int i=0;i<ARRLEN;i++)		//loop through the array
    	{
    		numbers[i]=findint(letters[i]);	//make a cast
    		std::cout<<numbers[i]<<", ";	//output the results
    	}
    
    	std::cout<<std::endl;			//newline and buffer flush
    	return 0;				//return to the OS
    }
    
    int findint(const char letter)
    {
    	if(letter=='0')
    		return 0;
    	else if(letter=='1')
    		return 1;
    	else if(letter=='2')
    		return 2;
    	else if(letter=='3')
    		return 3;
    	else if(letter=='4')
    		return 4;
    	else if(letter=='5')
    		return 5;
    	else if(letter=='6')
    		return 6;
    	else if(letter=='7')
    		return 7;
    	else if(letter=='8')
    		return 8;
    	else if(letter=='9')
    		return 9;
    
    	return -1;
    }
    Last edited by major_small; 10-19-2005 at 11:38 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Ancient Dragon
    Yes it is -- see this
    I knew that
    Quote Originally Posted by Dante Shamest
    I think it is. Perhaps you meant itoa()?
    Yeah, I was thinking about itoa(), and didn't bother to look up atoi()... I got thrown off yesterday because I'd always used atoi as standard code, and somebody pointed out that itoa (they said "itoa, etc") wasn't standard.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Ancient Dragon
    you just proved my point -- not every system uses ASCII formats, which means numeric digits may (or may not) be consecutive.
    Actually, the C standard states that the character set's digits from 0 to 9 shall be consecutive.

    Edit: At least this is the case in a C99 draft. Fortunately, all character sets are this way (at least this is the case for a practical value of 'all'.) :-)

    Edit 2: Excuse me, I forgot what board I'm on.
    Last edited by Rashakil Fol; 10-19-2005 at 12:09 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> numeric digits may (or may not) be consecutive

    The C++ standard guarantees that numeric digits are consecutive. In section 2.2 paragraph 3:
    In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
    which refers to this table in paragraph 1:
    Code:
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    0 1 2 3 4 5 6 7 8 9
    _ { } [ ] # ( ) < > % : ; . ? * + - /  & |  ! = , \ " 
    So for converting a single character digit to its integer version, subtracting '0' is guaranteed to work in any valid character set.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Convert to title case
    By gertie in forum C Programming
    Replies: 18
    Last Post: 07-06-2008, 10:58 PM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 11:53 AM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21