Carriage return (/r) missing when encoding and decoding a string using c#












-1















I have the following string :



This is the string to
test carriage return
using c#


The above string has two carriage returns in the end of lines. I have to encode this string to pass to a WCF service and then decode the string for further process.



//TO ENCODE
byte bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);


//TO DECODE
byte rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
string decodedStr = Encoding.UTF8.GetString(rawUtf8EncodedData);


After Encoding I get the below string :



This is the string tontest carriage returnnusing c#


After Decoding I get the below string :



This is the string tontest carriage returnnusing c#


Problem is, I need the carriage return back for some reason and not the n in my string after decoding. How can I achieve this ? The above string is only for illustration and in actual string, there would be lots of carriage returns and symbols










share|improve this question

























  • If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

    – D Stanley
    Nov 27 '18 at 2:03











  • I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

    – Anuya
    Nov 27 '18 at 2:07











  • Where do you "have the following string"? Line ends can be encoded in a variety of ways

    – Flydog57
    Nov 27 '18 at 2:34











  • Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

    – Jimi
    Nov 27 '18 at 2:35








  • 1





    Possible duplicate of What is a quick way to force CRLF in C# / .NET?

    – mjwills
    Nov 27 '18 at 2:52
















-1















I have the following string :



This is the string to
test carriage return
using c#


The above string has two carriage returns in the end of lines. I have to encode this string to pass to a WCF service and then decode the string for further process.



//TO ENCODE
byte bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);


//TO DECODE
byte rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
string decodedStr = Encoding.UTF8.GetString(rawUtf8EncodedData);


After Encoding I get the below string :



This is the string tontest carriage returnnusing c#


After Decoding I get the below string :



This is the string tontest carriage returnnusing c#


Problem is, I need the carriage return back for some reason and not the n in my string after decoding. How can I achieve this ? The above string is only for illustration and in actual string, there would be lots of carriage returns and symbols










share|improve this question

























  • If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

    – D Stanley
    Nov 27 '18 at 2:03











  • I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

    – Anuya
    Nov 27 '18 at 2:07











  • Where do you "have the following string"? Line ends can be encoded in a variety of ways

    – Flydog57
    Nov 27 '18 at 2:34











  • Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

    – Jimi
    Nov 27 '18 at 2:35








  • 1





    Possible duplicate of What is a quick way to force CRLF in C# / .NET?

    – mjwills
    Nov 27 '18 at 2:52














-1












-1








-1








I have the following string :



This is the string to
test carriage return
using c#


The above string has two carriage returns in the end of lines. I have to encode this string to pass to a WCF service and then decode the string for further process.



//TO ENCODE
byte bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);


//TO DECODE
byte rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
string decodedStr = Encoding.UTF8.GetString(rawUtf8EncodedData);


After Encoding I get the below string :



This is the string tontest carriage returnnusing c#


After Decoding I get the below string :



This is the string tontest carriage returnnusing c#


Problem is, I need the carriage return back for some reason and not the n in my string after decoding. How can I achieve this ? The above string is only for illustration and in actual string, there would be lots of carriage returns and symbols










share|improve this question
















I have the following string :



This is the string to
test carriage return
using c#


The above string has two carriage returns in the end of lines. I have to encode this string to pass to a WCF service and then decode the string for further process.



//TO ENCODE
byte bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);


//TO DECODE
byte rawUtf8EncodedData = Encoding.Default.GetBytes(encodedstring);
string decodedStr = Encoding.UTF8.GetString(rawUtf8EncodedData);


After Encoding I get the below string :



This is the string tontest carriage returnnusing c#


After Decoding I get the below string :



This is the string tontest carriage returnnusing c#


Problem is, I need the carriage return back for some reason and not the n in my string after decoding. How can I achieve this ? The above string is only for illustration and in actual string, there would be lots of carriage returns and symbols







c# .net string carriage-return






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 3:03









mjwills

15.6k42541




15.6k42541










asked Nov 27 '18 at 2:01









AnuyaAnuya

3,51837119203




3,51837119203













  • If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

    – D Stanley
    Nov 27 '18 at 2:03











  • I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

    – Anuya
    Nov 27 '18 at 2:07











  • Where do you "have the following string"? Line ends can be encoded in a variety of ways

    – Flydog57
    Nov 27 '18 at 2:34











  • Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

    – Jimi
    Nov 27 '18 at 2:35








  • 1





    Possible duplicate of What is a quick way to force CRLF in C# / .NET?

    – mjwills
    Nov 27 '18 at 2:52



















  • If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

    – D Stanley
    Nov 27 '18 at 2:03











  • I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

    – Anuya
    Nov 27 '18 at 2:07











  • Where do you "have the following string"? Line ends can be encoded in a variety of ways

    – Flydog57
    Nov 27 '18 at 2:34











  • Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

    – Jimi
    Nov 27 '18 at 2:35








  • 1





    Possible duplicate of What is a quick way to force CRLF in C# / .NET?

    – mjwills
    Nov 27 '18 at 2:52

















If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

– D Stanley
Nov 27 '18 at 2:03





If you are viewing the strings in the debugger, it will show you n instead of a carriage return. Try outputting the string to the console or a log file and see if the carriage returns are there.

– D Stanley
Nov 27 '18 at 2:03













I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

– Anuya
Nov 27 '18 at 2:07





I am using this string to do some logic, which will fail if n is there. It requires a carriage return to work. And right now it fails, because n is present in the raw string.

– Anuya
Nov 27 '18 at 2:07













Where do you "have the following string"? Line ends can be encoded in a variety of ways

– Flydog57
Nov 27 '18 at 2:34





Where do you "have the following string"? Line ends can be encoded in a variety of ways

– Flydog57
Nov 27 '18 at 2:34













Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

– Jimi
Nov 27 '18 at 2:35







Just tested (to be sure). Inserting rn or r or n, all are returned as the original. What do you mean with [the] string has two carriage returns? Are you extracting this string from a RichTextBox, maybe?.

– Jimi
Nov 27 '18 at 2:35






1




1





Possible duplicate of What is a quick way to force CRLF in C# / .NET?

– mjwills
Nov 27 '18 at 2:52





Possible duplicate of What is a quick way to force CRLF in C# / .NET?

– mjwills
Nov 27 '18 at 2:52












2 Answers
2






active

oldest

votes


















2














This is wrong:



byte bytes = Encoding.Default.GetBytes(rawstring);
string encodedStr = Encoding.UTF8.GetString(bytes);


The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.



The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.



If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.






share|improve this answer


























  • Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

    – Jimi
    Nov 27 '18 at 3:28



















-1














Replacing n with rn works fine






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491712%2fcarriage-return-r-missing-when-encoding-and-decoding-a-string-using-c-sharp%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This is wrong:



    byte bytes = Encoding.Default.GetBytes(rawstring);
    string encodedStr = Encoding.UTF8.GetString(bytes);


    The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.



    The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.



    If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.






    share|improve this answer


























    • Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

      – Jimi
      Nov 27 '18 at 3:28
















    2














    This is wrong:



    byte bytes = Encoding.Default.GetBytes(rawstring);
    string encodedStr = Encoding.UTF8.GetString(bytes);


    The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.



    The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.



    If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.






    share|improve this answer


























    • Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

      – Jimi
      Nov 27 '18 at 3:28














    2












    2








    2







    This is wrong:



    byte bytes = Encoding.Default.GetBytes(rawstring);
    string encodedStr = Encoding.UTF8.GetString(bytes);


    The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.



    The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.



    If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.






    share|improve this answer















    This is wrong:



    byte bytes = Encoding.Default.GetBytes(rawstring);
    string encodedStr = Encoding.UTF8.GetString(bytes);


    The first line gets bytes using the Default encoding. On .Net Core it's UTF-8 and you're likely okay, but on Windows it's the system code page from the OS, which could be anything.



    The second line of code then treats those bytes as if they are already in UTF-8 format, regardless of you what you actually got. This might work for a lot of strings, but eventually it's gonna fail spectacularly. Morever, the result is still a .Net string, which is the Unicode (UTF-16) encoding internally... so what you end up with is still UTF-16, not UTF-8.



    If you need to send a UTF-8 string to a network service, get the UTF-8 bytes using Encoding.UTF8.GetBytes(), and send the bytes as if they were a string.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 27 '18 at 3:29

























    answered Nov 27 '18 at 3:15









    Joel CoehoornJoel Coehoorn

    309k95494728




    309k95494728













    • Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

      – Jimi
      Nov 27 '18 at 3:28



















    • Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

      – Jimi
      Nov 27 '18 at 3:28

















    Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

    – Jimi
    Nov 27 '18 at 3:28





    Definitely. This conversion will not fail for a very limited sub-set of characters. Depending on the Local Encoding, possibly none. Outside these limited CodePoints, you get garbage.

    – Jimi
    Nov 27 '18 at 3:28













    -1














    Replacing n with rn works fine






    share|improve this answer




























      -1














      Replacing n with rn works fine






      share|improve this answer


























        -1












        -1








        -1







        Replacing n with rn works fine






        share|improve this answer













        Replacing n with rn works fine







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 2:20









        AnuyaAnuya

        3,51837119203




        3,51837119203






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53491712%2fcarriage-return-r-missing-when-encoding-and-decoding-a-string-using-c-sharp%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks