converting a string to it equivalent ascii value

This is a discussion on converting a string to it equivalent ascii value within the C Programming forums, part of the General Programming Boards category; Hi To All, I would like to convert a string into its equivalent ascii values, I know how to convert ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question converting a string to it equivalent ascii value

    Hi To All,
    I would like to convert a string into its equivalent ascii values, I know how to convert a char into its ascii value but the string has got me all stringed out!!

    I would be greatfull if someone could help me with my problem.

    thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    A string is just an array of chars. So the algorithm would be. Take the first character of the string, then convert it, do this until the end of the string is reached.

  3. #3
    Unregistered
    Guest
    Just wanted to compliment you for giving direction without handing him the code.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    What I'm trying to do is convert a string into ascii integer, so as I can add the integer values up, sorry if I was not to clear with my problem.

    thanks

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Re-read Shiro's post and then come back to this one if you're still stuck (he gave you the answer). Back already? Okay. Have a 'total' variable, initialized to zero. Increment the value of 'total' by the value of the first character in the string:
    Code:
    total += my_string[0];
    Now do the same thing for the rest of the characters in the string. This is an ideal place for a for loop.
    Jason Deckard

  6. #6
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    er...hope i don't get barked at for showing code...but it would be something on the lines of:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
    	char x[25];
    	int y,Total=0;
    
    	printf("\n\tWord: ");
    	scanf("%s",&x);
    	y=strlen(x);
    
    	while (y>=0)
    	{
    		Total=Total+x[y];
    		y--;
    	}
    
    	printf("\n\tTotal: %i\n\t",Total);
    }
    enjoy.

  7. #7
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    bark.

  8. #8
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    heh...i'm a beginner programming student too..and i get real bored so i like to throw other peoples code together for ...lame excuse....

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    11
    Wouldn't the strlen() return a value for the end of line char \0, if so wouldn't you want to start the loop ((y-1) >= 0). I ran across this thread and don't fully understand the strlen() myself.

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Smile

    Thanks very much guys I have now got my little problem sorted.

    thanks again

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    SavesTheDay: I'm sure you've been told this before, but void main is evil. The main function returns an int, nothing else.

    void main ( void ) /* Bad, evil, criminal */
    int main() /* Better, most common, but still not fully standard */
    int main ( void ) /* Correct */
    int main ( int argc, char *argv[] ) /* Correct */

    If you use void main your program is undefined and can do anything from work properly to launch nuclear missiles and cause the world to errupt into thermonuclear war (provied you have the proper hardware installed).

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    Are those functions contained in <3rdworldcountries.h>?

    Yeah...I get that *about the main function* alot....I'm in my first programming class this semester...and thats how they taught us to do it for some reason.........so i've been doing it...i'll try to stop.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by serious
    Wouldn't the strlen() return a value for the end of line char \0, if so wouldn't you want to start the loop ((y-1) >= 0). I ran across this thread and don't fully understand the strlen() myself.
    I think you are correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. string converting upper/lower
    By jlamn in forum C Programming
    Replies: 9
    Last Post: 09-24-2002, 06:01 PM

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