Convert char string to decimal equivalent (eg: a = chr(97))...

This is a discussion on Convert char string to decimal equivalent (eg: a = chr(97))... within the C Programming forums, part of the General Programming Boards category; Hi, I'm programming in C, on mandrake linux. I was wondering if there's any easy way to convert a string ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    Convert char string to decimal equivalent (eg: a = chr(97))...

    Hi, I'm programming in C, on mandrake linux. I was wondering if there's any easy way to convert a string of letters into their 'character code' (or decimal) equivalent? I know of one long way round, which would be to make a list of all of the characters and numbers equivalent to each other, then call that list for reference every time I wish to convert a character, but i'm sure it can be done another way.
    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    main(int argc, char *argv[])
    {
        //execl("/usr/bin/konqueror", " cnn.com", (char *) 0);
        
        //define char variable for the app name.
        char ichar[1024] = "konqueror ";
        //define a char variable for the command to be converted.
        char arbit[40] = "dir";
        
        //check we have 4 arguments in total.
        if (argc != 4)
        {
            printf("Incorrect usage!\n");
            return 0;
        }
        
        
        //add argv[1] to the end of the ichar string.
        strcat (ichar, argv[1]);
        //run the command.
        system(ichar);
    
        
        //convert string to decimal.
        /*THE FOLLOWING DOESNT WORK
        char my_string[] = "a";
        printf("%s = %d\n", my_string);
        printf ("%s\n", ichar);
        SO IT'S COMMENTED OUT*/
    
    return 0;
    }
    Any ideas? I heard atoi() converts a string, or character into an integer, but whatever character, or string i give it, it keeps returning 0, not the decimal equivalent.

    Any help would be much appreciated.

    Thanks.

    Blueprint.
    Last edited by Blueprint; 01-23-2005 at 09:05 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    So are you just trying to convert a character to its ASCII value? If so, then just do:

    Code:
    char somechar = 'a';
    int charval = somechar;
    printf("%c = %d\n",somechar,charval);
    To do that for an entire string, just loop through the above code for each char in the string. If you are trying to convert a string representation of a number into an integer, then you use atoi().
    Code:
    char str[] = "1234";
    int i = atoi(str);
    printf("%d\n",i);

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    Hi,

    I just want to say THANK YOU! That particular question's answer isn't easily found on the internet!!! The first code example was the one I was after:

    Code:
    char somechar = 'a';
    int charval = somechar;
    printf("%c = %d\n",somechar,charval);
    The atoi() just kept returning the integer 0 for every character I passed through it.

    Such simple methods are so easily overlooked!

    Thanks again!

    Blueprint.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    I send out the string "FFFD" trying to convert it to hits integer. How do I go about doing that?

    Is this right?
    Code:
    unsigned int DumpMode(unsigned char *start_addr,unsigned char *end_addr)
    {
    long pp;
    unsigned int putin[15];
    
    	 pp = atol(end_addr);
    	 printf("%ld",pp);
    	 return 0;
    }
    Last edited by NewGuy100; 08-17-2005 at 03:06 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Probably because "FFFD" is not a number.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    So what would be the best way to convert the Hex string into a number?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, prefix it with "0x" and use strtol, or do it by hand. Convert the letter or number yourself . Either way works.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Thx quzah, that really helps a lot

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Another one.
    Code:
    #include <stdio.h>
    
    void foo(const char *text)
    {
       unsigned int value;
       if ( sscanf(text, "%x", &value) == 1 )
       {
          printf("value = %u\n", value);
       }
    }
    
    int main(void)
    {
       foo("FFFD");
       return 0;
    }
    
    /* my output
    value = 65533
    */
    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.*

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Also helpful. Appreciate it

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    All you seem to want is something like this?

    Code:
    int main(int argc, char *argv[]) {
    	const char* rawr = "abcdefg";
    	for(int i = 0; i < strlen(rawr); i++)
    		printf("%d ", rawr[i]);
    	return 0;
    }
    Just to see the ASCII codes of characters? Sorry if I misread your question. You didn't seem interested in atoi or anything, I think you mentioned you just wanted to see the codes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. String Class Operations
    By Aidman in forum C++ Programming
    Replies: 10
    Last Post: 04-06-2003, 02:29 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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