How to manipulate ascii value of letters?

This is a discussion on How to manipulate ascii value of letters? within the C++ Programming forums, part of the General Programming Boards category; How to manipulate ascii value of letters? For example, if the ascii value of a and b is 0 and ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    How to manipulate ascii value of letters?

    How to manipulate ascii value of letters?
    For example, if the ascii value of a and b is 0 and 1, add 1 to the ascii value of a, then the character is changed from a to b.
    I know how to do it with array but it is clumsy.
    Is there any pre-defined function of certain library to get the ascii value of letters?I searched it and couldn't find any.
    Thanks in advance!
    Last edited by Roy01; 12-10-2006 at 09:13 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Could you elaborate on what you mean by "manipulate ascii value of letters"?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Quote Originally Posted by Dave_Sinkula
    Could you elaborate on what you mean by "manipulate ascii value of letters"?
    For example, if the ascii value of a and b is 0 and 1, add 1 to the ascii value of a, then the character is changed from a to b.
    I know how to do it with array but it is clumsy.
    Is there any pre-defined function of certain library to get the ascii value of letters?I searched it and couldn't find any.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    A character is already represented by its ASCII value. The letter 'a' is just the number 65. For example, run the following program on your computer:

    Code:
    #include <iostream>
    int main() {
      int x = 'a';
      std::cout << x << std::endl;
      return 0;
    }
    What do you see? Replace 'a' with 'a'+1 or 'c' or '1' * 2 and see what you get.

    'a' is (loosely speaking) just cute syntax for the number 65. The reason you use 'a' is because it's more readable than writing 65 in the middle of code.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Quote Originally Posted by Rashakil Fol
    A character is already represented by its ASCII value. The letter 'a' is just the number 65. For example, run the following program on your computer:

    Code:
    #include <iostream>
    int main() {
      int x = 'a';
      std::cout << x << std::endl;
      return 0;
    }
    What do you see? Replace 'a' with 'a'+1 or 'c' or '1' * 2 and see what you get.

    'a' is (loosely speaking) just cute syntax for the number 65. The reason you use 'a' is because it's more readable than writing 65 in the middle of code.
    That's exactly what I want! Thank you so much!

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    How about generating letters from ascii values?
    This is the sample input & output
    Code:
    Enter a word : abc
    Enter a number: 1
    The ciphered word: bcd
    What's wrong with my code?It can't get what I want.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    void main()
    {
    	
    	string input;
    	int asc[20];
    	char letter[20];
    
    	cout<<"Enter a word : ";
    	cin>>input;
    	input=letter;
    	for (int a=0;a<input.length();a++)
    	{
    		asc[a]=letter[a];
    	}
    
    	cout<<"Enter a number:  ";
    	int cip,c;
    	cin>>cip;
    
    	for (c=0;c<input.length();c++)
    	{
    			asc[c]=asc[c]+cip;
    
    			if (asc[c]>25)
    			{
    				asc[c]=asc[c]-25;
    			}		
    	}
    
    	for (int b=0;b<input.length();b++)
    	{
    		letter[b]=asc[b];
    	}
    	cout<<"The ciphered word: "<<letter;
    }
    Last edited by Roy01; 12-11-2006 at 12:56 AM.

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Code:
            cin>>input;
    	input=letter;
    Here the input was stored in "input". But in the next line you redeclare the "input" to be the same as undeclared "letter".Just don't use "letter" and operate solely on "input" and "asc". You should also change to this code:

    Code:
    	for (int a=0;a<input.length();a++)
    	{
    		asc[a]=input[a];
    	}
    Or better yet, use only the "input" variable and manipulate each char inside the variable.

    FYI, a char is actually a number between 0 to 255 (unsigned) or -127 to 128 (signed). You could say that it's a shorter version of integer. So, I think if you wanted to operate on char, use char. Although if your char is 255 and you add them, it will loop back to 0+the adder (CMIIW, forgot this one).

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Oops, I thought only letters have ascii values. Now it is going to be more complicated. Thank you!

  9. #9
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Oops, I thought only letters have ascii values. Now it is going to be more complicated. Thank you!
    Here's a link to an ASCII chart.

    In general, its the context that tells the program if a number represents an ASCII character. If there's a 49 somewhere in memory, it might represent 49, or it might represent an ASCII '1'. It might represent something else, like a machine-code instruction or the binary state of something. (Or the binary states of 8 things.) You and/or your program know what the variable is used for. cout will check the type, and if 49 is stored in a type-char it will automatically display 1.
    Last edited by DougDbug; 12-11-2006 at 05:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  2. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  3. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  4. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM
  5. HEEEELP: Letters to ASCII decimal equivalents?
    By Mazerius in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2002, 08:56 AM

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