[RESOLVED] Convert string to string literal verbatim
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] Convert string to string literal verbatim

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Convert string to string literal verbatim

    I have converted a PDF to byte[]. I then converted that to string since it specifically to be saved in the database as nvarchar(MAX). I would however like to know how to convert it to string literal verbatim (following the format of @"text here") to enable me to escape the characters present in the string so that it can be inserted into the database.

    Code:
    protected void SavePDFtoDB()
            {
                // Save PDF to byte[]
                mBytePDF = File.ReadAllBytes(msPath);
    
                // Convert byte[] to string
                String contents = ByteArrayToString(mBytePDF);
    
                // Save PDF in Database in form of string
                MainTable lMainTable = new MainTable();
                lMainTable.Date = DateTime.Now;
                lMainTable.Archive = 1;
                lMainTable.Text = @contents; // PROBLEM: Trying to convert to string literal verbatim
                SData.mWebService.setMainTable(lMainTable);
            }
    
            private string ByteArrayToString(byte[] arr)
            {
                System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
                return enc.GetString(arr);
            }
    Any help would be much appreciated!

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Convert string to string literal verbatim

    Convert the byte array to a base64 encoded string.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Re: Convert string to string literal verbatim

    Thank you, I used your suggested method. When I try to open the PDF I however get the error message that the data is presumably damaged. Could this have to do with converting it to a base64 encoded string?

    Code:
     protected void GeneratePDF(int value)
            {
                GetReport(miSelectedReportID);
    
                string lsTraineeName = SData.mWebService.getTrainee(SData.miTraineeID).Surname;
                string lsFileName = "Beurteilungsbogen_" + lsTraineeName + ".pdf";
    
                 // Create PDF on current path with doc name "test.pdf"
                string lsPDFpath = Server.MapPath("~/" + lsFileName);
    
                using (mStream = new FileStream(lsPDFpath, FileMode.Create))
                {
                      ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                      mBuffer = encoding.GetBytes(mMainTable.Text); 
    
                      Response.Clear();
                      Response.ClearContent();
                      Response.ClearHeaders();
                      Response.ContentType = "application/pdf";
                      Response.AddHeader("Content-Disposition", "attachment;filename=" + lsFileName);
                      Response.AddHeader("Content-Length", mBuffer.Length.ToString());
                      Response.OutputStream.Write(mBuffer, 0, mBuffer.Length);
                      //Response.BinaryWrite(mBuffer);
                      Response.Flush();
                      Response.End();
    
                      // To open in a new window: by saving pdf first
                      Response.Write("<script type='text/javascript'>");
                      Response.Write("window.open('" + "~/" + lsFileName + "','_blank')");
                      Response.Write("</script>");
                  }
            }

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Convert string to string literal verbatim

    ASCII is not base64. Yes, you've corrupted your data. Use the BitConverter or Convert class, i forget where it is exactly.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Re: Convert string to string literal verbatim

    Thanks!!!!! It works perfectly using:

    Code:
    try {
                        mBuffer = Convert.FromBase64String(mMainTable.Text);
                        // mBuffer = encoding.GetBytes(mMainTable.Text); 
                    } catch (FormatException) 
                    {
                       // Do nothing
                    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center