Encoding and culture issue.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vish Ba
    New Member
    • Jan 2012
    • 2

    Encoding and culture issue.

    I have written code to write data into .DAT file. When I was testing this application I found that on one of my test machine file generated like below, I am not getting how to avoid this.

    效摡牥〥㠰〬㠰ㄲ〰ㄳ㌷〬㠰ㄲ〰 ㄳ㌷㠬㈶ㄬㄯ⼴〲㈱ㄬㄯ⼴〲㈱䈬 剋ⱍ〰㈸〱㌰㜱ⰳ⼱ 㔱㈯㄰′㨲〳

    I am using following code to get data from class object and write into the file
    FileStream fs = new FileStream(pPat h + @"\" + pConfirmationNu mber + "-" + pHHID + "-" + OrderID + @".RDY", FileMode.Create , FileAccess.Writ e, FileShare.None) ;
    fs.Write(Encodi ng.ASCII.GetByt es(sOutput), 0, sOutput.Length) ;
    Last edited by Vish Ba; Jan 20 '12, 08:25 AM. Reason: Additional info
  • Fr33dan
    New Member
    • Oct 2008
    • 57

    #2
    Are you saying this works on one machine and not another? What are the difference between them, are they both running the same version of Windows and the .NET framework?

    Also why not use StreamWriter's constructor that accepts encoding as a property. Then you can avoid having to encode the string yourself:
    Code:
    StreamWriter fs = new StreamWriter(pPath + @"\" + pConfirmationNumber + "-" + pHHID + "-" + OrderID + @".RDY", Encoding.ASCII);
    fs.Write(sOutput);

    Comment

    • Vish Ba
      New Member
      • Jan 2012
      • 2

      #3
      Both machines are having same configuration. I also tried this by using Stream Writer class constructor but did not work for me. Please let me know what are the possible reasons for this issue. I am using this in Windows Service getting data from TCP clients. Please advice.

      Thanks.

      Comment

      • Fr33dan
        New Member
        • Oct 2008
        • 57

        #4
        I believe the answer is not in how you're writing the data but in how you're reading it. If I write the string you provided to a text file with Unicode encoding and then open it as a ASCII encoded file I get this:
        Code:
        ÿþHeader%008,00821003173,00821003173,862,1/14/2012,1/14/2012,BKRM,00821003173,1/15/2012 2: ·30
        Which looks like it might be your data.

        Comment

        Working...