Converting a char array into numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 6
Reputation: dj_saxy is an unknown quantity at this point 
Solved Threads: 0
dj_saxy's Avatar
dj_saxy dj_saxy is offline Offline
Newbie Poster

Converting a char array into numbers

 
0
  #1
Jan 16th, 2005
Is is possible to take an array full of characters and then make the characters into numbers?

i.e. a =1, b=2 etc.

I'm trying to write a program that deciphers ceasar shift ciphers.

Any ideas would be really helpful,

Thanks

dj_saxy
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,815
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Converting a char array into numbers

 
0
  #2
Jan 16th, 2005
  1. int get_code(char c)
  2. {
  3. string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  4. return static_cast<int>(alpha.find(c));
  5. }
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6
Reputation: dj_saxy is an unknown quantity at this point 
Solved Threads: 0
dj_saxy's Avatar
dj_saxy dj_saxy is offline Offline
Newbie Poster

Re: Converting a char array into numbers

 
0
  #3
Jan 16th, 2005
OK, i guess that it's a function so it should go at the start, but do i need to change anything, because my compiler doesn't like it...

Thanks for the help though
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,815
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Converting a char array into numbers

 
0
  #4
Jan 16th, 2005
>because my compiler doesn't like it...
Well, I was assuming some measure of intelligence on your part. I won't make that mistake in the future:
  1. #include <cctype>
  2. #include <iomanip>
  3. #include <iostream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int get_code(char c)
  9. {
  10. string alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  11. return static_cast<int>(alpha.find(c));
  12. }
  13.  
  14. int main()
  15. {
  16. string s = "This is a test";
  17.  
  18. cout<<"Code values of \""<< s <<"\" are:\n";
  19. for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
  20. if (isalpha(*it))
  21. cout<< left << setw(3) << get_code(*it);
  22. }
  23. cout<<endl;
  24. }
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: Converting a char array into numbers

 
0
  #5
Jan 17th, 2005
U can define a function called as " convert "
th prototype will look like this : int convert( char)
for(i=0;i<MAX;i++)
number = convert(a[i]);

int convert(char c)
{ return c - '0' ;
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,815
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Converting a char array into numbers

 
0
  #6
Jan 17th, 2005
U can define a function called as " convert "
th prototype will look like this : int convert( char)
for(i=0;i<MAX;i++)
number = convert(a[i]);

int convert(char c)
{ return c - '0' ;
}
So, do you just post random replies without reading the original question? Come to think of it, is this trick the only one you know? It seems like the only thing you post.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC