Click here to Skip to main content
12,255,210 members (61,828 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 28-Sep-12 16:52pm
Edited 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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

By string, I believe you really mean char.
If I am correct you can achieve this quickly with int c = Convert.ToInt32('a');
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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:
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
  Permalink  
v5
Comments
   
Thank you very much, Meysam.
--SA
CPallini 29-Sep-12 10:26am
   
My 5.
   
Thank you, Carlo.
--SA
Maciej Los 29-Sep-12 10:39am
   
+5!
   
Thank you, Maciej.
--SA
Marcus Kramer 29-Sep-12 12:21pm
   
+5. Naturally this is the complete solution.
   
Thank you, Marcus.
The complete? ...well, if I also put 2 references to Unicode.org, general and on UTFs, plus 2-3 references to Unicode topics, plus the ellipsis could be replaced with a sample of encoding and decoding code, plus key distribution (RSA is open-key), plus encryption references, then, may be... :-)
--SA
Espen Harlinn 29-Sep-12 12:29pm
   
5'ed!
   
Thank you, Espen.
--SA
Abhinav S 30-Sep-12 0:45am
   
My 5.
   
Thank you, Abhinav.
--SA

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100