How to convert a byte array to string?












0















I just finished creating a huffman compression algorithm . I converted my compressed text from a string to a byte array with bytearray(). Im attempting to decompress my huffman algorithm. My only concern though is that i cannot convert my byte array back into a string. Is there any built in function i could use to convert my byte array (with a variable) back into a string? If not is there a better method to convert my compressed string to something else? I attempted to use byte_array.decode() and I get this:



print("Index: ", Index) # The Index


# Subsituting text to our compressed index

for x in range(len(TextTest)):

TextTest[x]=Index[TextTest[x]]


NewText=''.join(TextTest)

# print(NewText)
# NewText=int(NewText)


byte_array = bytearray() # Converts the compressed string text to bytes
for i in range(0, len(NewText), 8):
byte_array.append(int(NewText[i:i + 8], 2))


NewSize = ("Compressed file Size:",sys.getsizeof(byte_array),'bytes')

print(byte_array)

print(byte_array)

print(NewSize)

x=bytes(byte_array)
x.decode()


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte










share|improve this question

























  • You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

    – martineau
    Nov 21 '18 at 7:15













  • Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

    – Mohamed Alremeithi
    Nov 23 '18 at 10:11













  • That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

    – martineau
    Nov 23 '18 at 10:43


















0















I just finished creating a huffman compression algorithm . I converted my compressed text from a string to a byte array with bytearray(). Im attempting to decompress my huffman algorithm. My only concern though is that i cannot convert my byte array back into a string. Is there any built in function i could use to convert my byte array (with a variable) back into a string? If not is there a better method to convert my compressed string to something else? I attempted to use byte_array.decode() and I get this:



print("Index: ", Index) # The Index


# Subsituting text to our compressed index

for x in range(len(TextTest)):

TextTest[x]=Index[TextTest[x]]


NewText=''.join(TextTest)

# print(NewText)
# NewText=int(NewText)


byte_array = bytearray() # Converts the compressed string text to bytes
for i in range(0, len(NewText), 8):
byte_array.append(int(NewText[i:i + 8], 2))


NewSize = ("Compressed file Size:",sys.getsizeof(byte_array),'bytes')

print(byte_array)

print(byte_array)

print(NewSize)

x=bytes(byte_array)
x.decode()


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte










share|improve this question

























  • You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

    – martineau
    Nov 21 '18 at 7:15













  • Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

    – Mohamed Alremeithi
    Nov 23 '18 at 10:11













  • That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

    – martineau
    Nov 23 '18 at 10:43
















0












0








0








I just finished creating a huffman compression algorithm . I converted my compressed text from a string to a byte array with bytearray(). Im attempting to decompress my huffman algorithm. My only concern though is that i cannot convert my byte array back into a string. Is there any built in function i could use to convert my byte array (with a variable) back into a string? If not is there a better method to convert my compressed string to something else? I attempted to use byte_array.decode() and I get this:



print("Index: ", Index) # The Index


# Subsituting text to our compressed index

for x in range(len(TextTest)):

TextTest[x]=Index[TextTest[x]]


NewText=''.join(TextTest)

# print(NewText)
# NewText=int(NewText)


byte_array = bytearray() # Converts the compressed string text to bytes
for i in range(0, len(NewText), 8):
byte_array.append(int(NewText[i:i + 8], 2))


NewSize = ("Compressed file Size:",sys.getsizeof(byte_array),'bytes')

print(byte_array)

print(byte_array)

print(NewSize)

x=bytes(byte_array)
x.decode()


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte










share|improve this question
















I just finished creating a huffman compression algorithm . I converted my compressed text from a string to a byte array with bytearray(). Im attempting to decompress my huffman algorithm. My only concern though is that i cannot convert my byte array back into a string. Is there any built in function i could use to convert my byte array (with a variable) back into a string? If not is there a better method to convert my compressed string to something else? I attempted to use byte_array.decode() and I get this:



print("Index: ", Index) # The Index


# Subsituting text to our compressed index

for x in range(len(TextTest)):

TextTest[x]=Index[TextTest[x]]


NewText=''.join(TextTest)

# print(NewText)
# NewText=int(NewText)


byte_array = bytearray() # Converts the compressed string text to bytes
for i in range(0, len(NewText), 8):
byte_array.append(int(NewText[i:i + 8], 2))


NewSize = ("Compressed file Size:",sys.getsizeof(byte_array),'bytes')

print(byte_array)

print(byte_array)

print(NewSize)

x=bytes(byte_array)
x.decode()


UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte







python arrays decompression huffman-code bytestring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 13:25







Mohamed Alremeithi

















asked Nov 21 '18 at 6:45









Mohamed AlremeithiMohamed Alremeithi

13




13













  • You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

    – martineau
    Nov 21 '18 at 7:15













  • Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

    – Mohamed Alremeithi
    Nov 23 '18 at 10:11













  • That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

    – martineau
    Nov 23 '18 at 10:43





















  • You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

    – martineau
    Nov 21 '18 at 7:15













  • Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

    – Mohamed Alremeithi
    Nov 23 '18 at 10:11













  • That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

    – martineau
    Nov 23 '18 at 10:43



















You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

– martineau
Nov 21 '18 at 7:15







You can convert it to a string by calling the bytearray.decode() method and supplying an encoding. For example: byte_array.decode('ascii'). If you leave the decoding argument out, it will default to 'utf-8'.

– martineau
Nov 21 '18 at 7:15















Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

– Mohamed Alremeithi
Nov 23 '18 at 10:11







Hey, I got this when i added your code: byte_array.decode('ascii') UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 0: ordinal not in range(128). When I removed the 'ascii' part I got:UnicodeDecodeError: 'utf-8' codec can't decode byte 0x88 in position 0: invalid start byte

– Mohamed Alremeithi
Nov 23 '18 at 10:11















That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

– martineau
Nov 23 '18 at 10:43







That means the data in your byte array doesn't contain valid characters in those encodings. You need to find an acceptable one. There's some here in documentation—'hex' might be good. You can also use 'latin1' which maps the code points 0–255 to the bytes 0x0–0xff. Doing so will allow you to convert the result back to bytes later by using the_string.encode('latin1'). I first heard about doing this in this answer to a unrelated question (to solve a different problem).

– martineau
Nov 23 '18 at 10:43














1 Answer
1






active

oldest

votes


















0














You can use .decode('ascii') (leave empty for utf-8).



print(bytearray("abcd", 'utf-8').decode())
>>> abcd


Source : Convert bytes to a string?






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%2f53406587%2fhow-to-convert-a-byte-array-to-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use .decode('ascii') (leave empty for utf-8).



    print(bytearray("abcd", 'utf-8').decode())
    >>> abcd


    Source : Convert bytes to a string?






    share|improve this answer




























      0














      You can use .decode('ascii') (leave empty for utf-8).



      print(bytearray("abcd", 'utf-8').decode())
      >>> abcd


      Source : Convert bytes to a string?






      share|improve this answer


























        0












        0








        0







        You can use .decode('ascii') (leave empty for utf-8).



        print(bytearray("abcd", 'utf-8').decode())
        >>> abcd


        Source : Convert bytes to a string?






        share|improve this answer













        You can use .decode('ascii') (leave empty for utf-8).



        print(bytearray("abcd", 'utf-8').decode())
        >>> abcd


        Source : Convert bytes to a string?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 7:18









        Dorian TurbaDorian Turba

        223214




        223214
































            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%2f53406587%2fhow-to-convert-a-byte-array-to-string%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()