I am trying to convert a char array into an int array, does anyone know how to start setting up the loop?
Jae
I am trying to convert a char array into an int array, does anyone know how to start setting up the loop?
Jae
depends on the char array. post an example of the array, such as
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";
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"; char buf[2] = {0} buf[0] = array[0]; int n = atoi(buf);
Code:char array[] = "12345"; int n = array[0] - '0';
Thanks Ancient Dragon, that solved my problem.
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)48Originally Posted by Ancient Dragon
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: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.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 }
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
you just proved my point -- not every system uses ASCII formats, which means numeric digits may (or may not) be consecutive.Originally Posted by major_small
Yes it is -- see thisOriginally Posted by major_small
Last edited by Ancient Dragon; 10-19-2005 at 11:31 AM.
I think it is. Perhaps you meant itoa()?Originally Posted by major_small
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
I knew thatOriginally Posted by Ancient Dragon
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.Originally Posted by Dante Shamest
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
Actually, the C standard states that the character set's digits from 0 to 9 shall be consecutive.Originally Posted by Ancient Dragon
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.
>> numeric digits may (or may not) be consecutive
The C++ standard guarantees that numeric digits are consecutive. In section 2.2 paragraph 3:which refers to this table in paragraph 1: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.So for converting a single character digit to its integer version, subtracting '0' is guaranteed to work in any valid character set.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 _ { } [ ] # ( ) < > % : ; . ? * + - / ˆ & | ˜ ! = , \ " ’