Extended Ascii charaters are not written in the text file....
-
Hi all, I am trying to convert a byte array in to a character array and trying to write the character array in a text file... The character set till 127 is propetly written.. whereas all the characters which is above 127 is modified as '?' i.e ascii number 63..... How can i write this to the text file... the code snippet..
Byte[] encryptedData = myrsa.Encrypt(newdata, false);
Char[] asciiarray = encoding.GetChars(encryptedData);
FileStream fs = File.Open(FilePath,filemode,FileAccess.Write);
sw = new StreamWriter(fs, Encoding.ASCII );
sw.WriteLine(new string (asciiarray));
sw.Close();The above code writes only ascii characters whose values are less than 127 whereas all other characters are automatically changed to '?' Googled, but no fruits......
Have a Happy Coding.....
-
Hi all, I am trying to convert a byte array in to a character array and trying to write the character array in a text file... The character set till 127 is propetly written.. whereas all the characters which is above 127 is modified as '?' i.e ascii number 63..... How can i write this to the text file... the code snippet..
Byte[] encryptedData = myrsa.Encrypt(newdata, false);
Char[] asciiarray = encoding.GetChars(encryptedData);
FileStream fs = File.Open(FilePath,filemode,FileAccess.Write);
sw = new StreamWriter(fs, Encoding.ASCII );
sw.WriteLine(new string (asciiarray));
sw.Close();The above code writes only ascii characters whose values are less than 127 whereas all other characters are automatically changed to '?' Googled, but no fruits......
Have a Happy Coding.....
Converting from a byte[] to a char[] is odd, because the two values are not the same at all. The bytes you (correctly I think) have in your encrypted data are unsigned 8 bit quantities. Chars are 16 bit unicode quantities. Since you are then trying to write these to an ASCII encoded (7 or 8 bit depending on who you talk to) there is some oddness here. Why not just write the byte[] to a normal filestream? Or encrypt it into the filestream directly? There is a cryptostream, after all... Here is my generic encrypt routine (uses RSATriple)
public void Encrypt(Stream sIn, Stream sOut) { /\* \* Notes: \* We do NOT close sIn or sOut. \* Since these could be MemoryStreams, closing them \* would throw away the data! \* It is good practice to decrypt to memory, not file! \* \* We don't work with files bigger than an int full. \* It would be fairly easy to modify this to block the I/O \* \* We make the output bigger than it needs to be to flush \* all the "real" data to the output. \* (This should not be necessary, but when decrypting to a \* MemoryStream you have to close it to get the last block. \* Don't ask me why - ask MS.) \*/ string Routine = "Encrypt: "; if (key == null) { throw new Exception(Routine + EX\_CRYPTO\_NOKEY); } if (sIn.Length > int.MaxValue) { throw new Exception(Routine + EX\_CRYPTO\_TOOBIG); } sOut.Write(CreateBlockHeader(sIn), 0, encAlg.BlockSize); CryptoStream encrypt = new CryptoStream(sOut, encAlg.CreateEncryptor(), CryptoStreamMode.Write); int inputLength = (int)sIn.Length + encAlg.BlockSize; // Make it too big! byte\[\] abIn = new byte\[inputLength\]; sIn.Read(abIn, 0, inputLength); encrypt.Write(abIn, 0, inputLength); encrypt.FlushFinalBlock(); key.Reset(); if (sIn.CanSeek) { sIn.Seek(0, SeekOrigin.Begin); } if (sOut.CanSeek) { sOut.Seek(0, SeekOrigin.Begin); } }
[EDIT]Edited to correct spelling errors caused by dyslexic keyboard[/EDIT]
No tr
-
Converting from a byte[] to a char[] is odd, because the two values are not the same at all. The bytes you (correctly I think) have in your encrypted data are unsigned 8 bit quantities. Chars are 16 bit unicode quantities. Since you are then trying to write these to an ASCII encoded (7 or 8 bit depending on who you talk to) there is some oddness here. Why not just write the byte[] to a normal filestream? Or encrypt it into the filestream directly? There is a cryptostream, after all... Here is my generic encrypt routine (uses RSATriple)
public void Encrypt(Stream sIn, Stream sOut) { /\* \* Notes: \* We do NOT close sIn or sOut. \* Since these could be MemoryStreams, closing them \* would throw away the data! \* It is good practice to decrypt to memory, not file! \* \* We don't work with files bigger than an int full. \* It would be fairly easy to modify this to block the I/O \* \* We make the output bigger than it needs to be to flush \* all the "real" data to the output. \* (This should not be necessary, but when decrypting to a \* MemoryStream you have to close it to get the last block. \* Don't ask me why - ask MS.) \*/ string Routine = "Encrypt: "; if (key == null) { throw new Exception(Routine + EX\_CRYPTO\_NOKEY); } if (sIn.Length > int.MaxValue) { throw new Exception(Routine + EX\_CRYPTO\_TOOBIG); } sOut.Write(CreateBlockHeader(sIn), 0, encAlg.BlockSize); CryptoStream encrypt = new CryptoStream(sOut, encAlg.CreateEncryptor(), CryptoStreamMode.Write); int inputLength = (int)sIn.Length + encAlg.BlockSize; // Make it too big! byte\[\] abIn = new byte\[inputLength\]; sIn.Read(abIn, 0, inputLength); encrypt.Write(abIn, 0, inputLength); encrypt.FlushFinalBlock(); key.Reset(); if (sIn.CanSeek) { sIn.Seek(0, SeekOrigin.Begin); } if (sOut.CanSeek) { sOut.Seek(0, SeekOrigin.Begin); } }
[EDIT]Edited to correct spelling errors caused by dyslexic keyboard[/EDIT]
No tr
Hi Thank You so much for the valuable input... But even in a different encoding style(UTF-8, UTF-32 etc) the characters are not written to the file...... Any clues on that>???
Have a Happy Coding.....
-
Hi Thank You so much for the valuable input... But even in a different encoding style(UTF-8, UTF-32 etc) the characters are not written to the file...... Any clues on that>???
Have a Happy Coding.....
Because the data you start with is not UTF, or ASCII or any other character set based data. It is raw bytes. Unsigned 8 bit values. Why convert these? In your example:
Byte[] encryptedData = myrsa.Encrypt(newdata, false);
Char[] asciiarray = encoding.GetChars(encryptedData);
using (StreamWriter sw = new StreamWriter(filepath, FileMode.Create))
{
sw.Write(encryptedData, 0, encryptedData.Length);
}No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Hi all, I am trying to convert a byte array in to a character array and trying to write the character array in a text file... The character set till 127 is propetly written.. whereas all the characters which is above 127 is modified as '?' i.e ascii number 63..... How can i write this to the text file... the code snippet..
Byte[] encryptedData = myrsa.Encrypt(newdata, false);
Char[] asciiarray = encoding.GetChars(encryptedData);
FileStream fs = File.Open(FilePath,filemode,FileAccess.Write);
sw = new StreamWriter(fs, Encoding.ASCII );
sw.WriteLine(new string (asciiarray));
sw.Close();The above code writes only ascii characters whose values are less than 127 whereas all other characters are automatically changed to '?' Googled, but no fruits......
Have a Happy Coding.....
You can't use GetChars to decode the encrypted data into text, as it's not text that was encoded in the first place. If you want to represent the data as text, you can for example use base64 encoding:
string text = Convert.ToBase64String(encryptedData);
If you want to save the data to the file without changing it, you should write it as bytes, not as text:
File.WriteAllBytes(FilePath, encryptedData);
Despite everything, the person most likely to be fooling you next is yourself.
-
You can't use GetChars to decode the encrypted data into text, as it's not text that was encoded in the first place. If you want to represent the data as text, you can for example use base64 encoding:
string text = Convert.ToBase64String(encryptedData);
If you want to save the data to the file without changing it, you should write it as bytes, not as text:
File.WriteAllBytes(FilePath, encryptedData);
Despite everything, the person most likely to be fooling you next is yourself.
Yes!!! exactly! I have modified the code in the following way...
Byte[] encryptedData = myrsa.Encrypt(newdata, false);
FileStream fs = File.Open(FilePath,filemode,FileAccess.Write);
sw = new StreamWriter(fs, Encoding.ASCII );
sw.WriteLine(Convert.ToBase64String(encryptedData));Thank you all for the help!!!
Have a Happy Coding.....