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.
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.
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
KurtCode:char * str = "HELLO"; int i, cnt = strlen(str); for ( i = 0; i < cnt; ++I ) printf("%d,", (int)str[i]);
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.
Thanks for the reply
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
XOR operations are performed on integral values. Strings are not integral values.
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".
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
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
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
Note that in order for XOR encryption to be effective the key length should be equal to the length of the clear text.