Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Extended Ascii charaters are not written in the text file....

Extended Ascii charaters are not written in the text file....

Scheduled Pinned Locked Moved C#
questioncssdata-structures
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    King Julien
    wrote on last edited by
    #1

    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.....

    OriginalGriffO G 2 Replies Last reply
    0
    • K King Julien

      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.....

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      K 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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

        K Offline
        K Offline
        King Julien
        wrote on last edited by
        #3

        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.....

        OriginalGriffO 1 Reply Last reply
        0
        • K King Julien

          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.....

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • K King Julien

            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.....

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            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.

            K 1 Reply Last reply
            0
            • G Guffa

              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.

              K Offline
              K Offline
              King Julien
              wrote on last edited by
              #6

              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.....

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups