Thread: How to find ASCII value of string

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    47

    How to find ASCII value of string

    I know how to find find ASCII value of given character, but I am not getting how to find ASCII value of given string. For example I want to find ASCII value of string "HELLO",so how to do that.

    Thanks a lot in advance for your replies.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You cannot find the ascii value of a complete string
    to get the ascii value of a single character you could just cast it to an int


    Code:
    char * str = "HELLO";
    int i, cnt = strlen(str);
    for ( i = 0; i < cnt; ++I )
        printf("%d,", (int)str[i]);
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Strings do not have ASCII values, individual characters do.

    Converting a character to a int only gives its ASCII value if the implementation (compiler, library, host system) uses the ASCII character set. Not all implementations do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Thanks for the reply

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Thanks for the reply
    so what to do for getting ASCII value of given string"HELLO",actually I want to do XOR operation on given to strings

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    XOR operations are performed on integral values. Strings are not integral values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    I want to do Encryption of two Strings, so I need to find their ASCII value first and then do XOR operation on that

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by shyjuu View Post
    I want to do Encryption of two Strings, so I need to find their ASCII value first and then do XOR operation on that
    Look at the previous posts in this thread. Your problem description is meaningless.

    If your requirement is to XOR the characters in the two strings (say the first "encrypted" character is obtained by XORing the first character from each of the two input strings), then simply XOR individual characters using a loop. But no part of that involves an act described as "getting ASCII value of a string" (there is no such operation) let alone doing an XOR "on that".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by grumpy View Post
    Look at the previous posts in this thread. Your problem description is meaningless.

    If your requirement is to XOR the characters in the two strings (say the first "encrypted" character is obtained by XORing the first character from each of the two input strings), then simply XOR individual characters using a loop. But no part of that involves an act described as "getting ASCII value of a string" (there is no such operation) let alone doing an XOR "on that".
    so for XOR operation I need the decimal value so I was asking, is there any way to directly perform XOR operation on characters???

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by shyjuu View Post
    so for XOR operation I need the decimal value so I was asking, is there any way to directly perform XOR operation on characters???
    yes

    Code:
        int i;
        char * str = "HELLO";
        char ch = 'A';
        for ( i = 0;str[i] != 0; ++i )
        {
          char temp = str[i] ^ ch;
          printf("%c:%02x\n",temp ,temp );
        }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    47
    Quote Originally Posted by vart View Post
    yes

    Code:
        int i;
        char * str = "HELLO";
        char ch = 'A';
        for ( i = 0;str[i] != 0; ++i )
        {
          char temp = str[i] ^ ch;
          printf("%c:%02x\n",temp ,temp );
        }
    I am not able to understand the output , can u please explain, and especially the last printf statement

  12. #12
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by shyjuu View Post
    I am not able to understand the output , can u please explain, and especially the last printf statement
    Characters are technically integers. He used the hex conversion character in this case ( lowercase x ), which could've also been an uppercase X depending on preference. Then, he created a string literal, and set a char variable to an 'A' for some data to initialize to. He looped through the string until the NULL termination character was reached. On each iteration, he set temporary holder value to the XOR'ed value of the constant string "HELLO" and 'A'. He then printed out the the actual encrypted character with the value in lowercase hex ( unsigned int ).

    Personally I would've done the same thing this way( with the exception of using a value that varies for the XOR'ing ) :

    Code:
       
    int offset; 
    char * str = "HELLO";
    
    for ( offset = 0; str[offset]; offset++ )
          printf("%c : %02X\n", (str[offset] ^ offset) , (str[offset] ^ offset) );
    Last edited by HelpfulPerson; 08-25-2013 at 11:30 AM.
    "Some people think they can outsmart me, maybe. Maybe. I've yet to meet one that can outsmart bullet" - Meet the Heavy, Team Fortress 2

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    output shows that xoring two perfectly printable characters - you with big chance get some non-printable characters.

    So after xoring you will need something like base64 encoding to get some nicely printable garbage string similar to the following

    Code:
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.17 (MingW32)
    
    iQEcBAEBAgAGBQJSGXk9AAoJEAgV1kHhjGOoYssIAMHK0RkcHRwLY4Q1Hpjj6jlH
    Cl5SCDGFq+gN6m76X4PBMAUWesvDyh6kQo97VR2UjmboVa9mAQFcKj+T9jGpwe9E
    PvMKv1tYFgWC+j3+uzcl0qQGpaZ5KpsvcjZgQrltkA/r0NrOm7QIe7EIIAp6Nw7x
    WSv5sXUcmewzUHoijbNigd1YNTGoSgH3aVbgRW2IaBSl2Ym+tG4UQakUbu5LWpa5
    Jhngwwp8AnzvkBwTi2JIiy77kNaD+cDUbo/JH6I1lsSCOfsSrIvzij4WzdWBwLO+
    63PkcZuScwY8ewbF5yfPUKMpAY1BXXUtVkI6L2wIdXa2+CKppW1UBQqQqas3+uU=
    =gld1
    -----END PGP SIGNATURE-----
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Note that in order for XOR encryption to be effective the key length should be equal to the length of the clear text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-24-2012, 12:31 AM
  2. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  3. Convert ASCII to a string
    By Toonzaka22 in forum C Programming
    Replies: 2
    Last Post: 11-29-2009, 09:59 PM
  4. ASCII to string
    By tyroiusrtmaonm in forum C++ Programming
    Replies: 2
    Last Post: 05-08-2007, 08:45 PM
  5. ASCII hex string to __int64
    By Drewpee in forum Windows Programming
    Replies: 3
    Last Post: 03-06-2002, 12:13 PM

Tags for this Thread