Click here to Skip to main content
Rate this: bad
good
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 17:52pm
Edited 29-Sep-12 5:06am
v2
Comments
Ritu khatri at 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 at 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 at 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 at 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 at 29-Sep-12 10:46am
   
Sergey, you might right, but formally the answer is correct ;)
So, my vote is 5!
Abhinav S at 30-Sep-12 0:46am
   
Thank you.
Espen Harlinn at 29-Sep-12 12:29pm
   
5'ed!
Abhinav S at 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
Sergey Alexandrovich Kryukov at 30-Sep-12 9:45am
   
Thank you very much, Meysam.
--SA
CPallini at 29-Sep-12 10:26am
   
My 5.
Sergey Alexandrovich Kryukov at 30-Sep-12 9:45am
   
Thank you, Carlo.
--SA
Maciej Los at 29-Sep-12 10:39am
   
+5!
Sergey Alexandrovich Kryukov at 30-Sep-12 9:45am
   
Thank you, Maciej.
--SA
Marcus Kramer at 29-Sep-12 12:21pm
   
+5. Naturally this is the complete solution.
Sergey Alexandrovich Kryukov at 30-Sep-12 9:49am
   
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 at 29-Sep-12 12:29pm
   
5'ed!
Sergey Alexandrovich Kryukov at 30-Sep-12 9:50am
   
Thank you, Espen.
--SA
Abhinav S at 30-Sep-12 0:45am
   
My 5.
Sergey Alexandrovich Kryukov at 30-Sep-12 9:50am
   
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
0 Sergey Alexandrovich Kryukov 408
1 Sascha Lefévre 235
2 Maciej Los 180
3 Abhinav S 170
4 Richard Deeming 155
0 Sergey Alexandrovich Kryukov 7,096
1 OriginalGriff 6,395
2 Maciej Los 2,782
3 Abhinav S 2,732
4 Peter Leow 2,694


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2015
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