Thread: convert string to ASCII value

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Question convert string to ASCII value

    I was new to C++. I trying to convert a string(example : hello) into ASCII value. I tried to use putchar(0, but it only work for one character.

    if I want to declare the char string become char string[80], then what are the following steps should I take??? Thank you for your time.

    # include <iostream.h>
    # include <string.h>
    # include <stdio.h>

    void main(){

    char string;

    cout<<"Enter a string : ";
    cin>>string;

    cout<<"The string you entered is "<<string<<endl;
    cout<<"The ASCII value for that string is "<<putchar(string);

    }

  2. #2

    Re: convert string to ASCII value

    Code:
    char string[80];
    
    cout<<"Enter a string : ";
    cin.getline(string,80,'\n');

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    12

    Unhappy display ASCII vaue of a string

    # include <iostream.h>

    void main(){

    char string[80];

    cout<<"Enter a string : ";
    cin.getline(string,80,'\n');


    cout<<"The string you entered is "<<string<<endl;
    cout<<"The ASCII value for that string is "<<string;

    }

    if what I want to display is the ASCII value of H+e+l+l+o = ??, which line in the coding should I change? thank you.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You want the sum of the ASCII values? Use a for loop:
    Code:
    #include <string.h>
    
    int ReturnASCIISum(char* String)
    {
       int TotalSum = 0;
       for(int i=0; i<strlen(String); i++)
       {
          TotalSum += (int)String[i];
       }
       return TotalSum;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM