Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I want to convert any string to its ASCII format for rsa algorithm implementation in C# and the converted value is store in a single Integer; So please help me.
Posted
Updated 29-Sep-12 4:06am
v2
Comments
Ritu khatri 28-Sep-12 23:03pm    
No it dioen't meant that it is a char , it is a string

By string, I believe you really mean char.
If I am correct you can achieve this quickly with int c = Convert.ToInt32('a');
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 23:17pm    
Marcus, you see, this is formally answers to OP's question, but this answer is not useful, if you remember that OP wants to do encryption. You probably did not pay attention for that. And the question is based on a totally wrong conception of how a text should be used with encryption.

Please see my answer where I think I explained it all and provided correct recipe.
--SA
Maciej Los 29-Sep-12 10:45am    
Sergey, you might right, but formally the answer is correct ;)
So, my vote is 5!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Sep-12 23:18pm    
Abhinav,

Тhis is formally answers to OP's question, but this answer is not useful, if you remember that OP wants to do encryption. You probably did not pay attention for that. And the question is based on a totally wrong conception of how a text should be used with encryption.

Please see my answer where I think I explained it all and provided correct recipe.
--SA
Maciej Los 29-Sep-12 10:46am    
Sergey, you might right, but formally the answer is correct ;)
So, my vote is 5!
Abhinav S 30-Sep-12 0:46am    
Thank you.
Espen Harlinn 29-Sep-12 12:29pm    
5'ed!
Abhinav S 30-Sep-12 0:45am    
Thank you.
You absolutely don't need to do it to use encryption (RSA or whatever). You really need to preserve encoding. All encryption algorithms work with arrays of bytes and nothing else. And this array of bytes has nothing to do with ASCII. Moreover, converting Unicode (in one or another UTF; Windows uses UTF-16LE for memory presentation of text) to ASCII means the loss of information. All your code points fit in ASCII? Possibly, but don't be so sure. Use UTF-8: it takes the same space as ASCII for all code points < 128 bit still supports all code points including 32-bit ones.

Generally, I would advise you to forget ASCII: it's gone; the ghost of it only exists as a Unicode subset. Even US English text contains code points beyond ASCII, if good typography is used. Did you know that?

So, finally, how to get that array of bytes? Simple. For example, let's assume I convinced you to use UTF-8. Then the code is:
C#
string sourceText = //... this is your input
byte[] encryptionInput = System.Text.Encoding.UTF8.GetBytes(sourceText);

//now encrypt encryptionInput...

//later decrypt it, obtain some
byte[] decryptionOutput = //decrypt

string decryptedText = new string(System.Text.Encoding.UTF8.GetChars(decryptionOutput));
// if everything is correct, it should give decryptedText == sourceText


This is how it's done. Instead of System.Text.Encoding.UTF8 encoding, you can use any other, even ASCII, but I strongly recommend UTF-8, by many reasons.

—SA
 
Share this answer
 
v5
Comments
[no name] 29-Sep-12 10:08am    
Great job!
+5
Sergey Alexandrovich Kryukov 30-Sep-12 9:45am    
Thank you very much, Meysam.
--SA
CPallini 29-Sep-12 10:26am    
My 5.
Sergey Alexandrovich Kryukov 30-Sep-12 9:45am    
Thank you, Carlo.
--SA
Maciej Los 29-Sep-12 10:39am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900