Java: Converting byte string to byte array









up vote
0
down vote

favorite












I want to convert my byte to a String, and then convert that String to a byte.



So,



byte b = myFunction();
String bstring = b.toString();
/* Here the methode to convert the bstring to byte, and call it ser */
String deser = new String(ser);


bstring gives me [B@74e752bb.



And then convert the String to byte. I'm not using it in this order, but this is an example.



How do I need to do this in Java?



Kind regards,



Stijn










share|improve this question



















  • 1




    why not new String(b) and what about bstring.getBytes()?
    – nullpointer
    Nov 10 at 15:09










  • Take a look at here: stackoverflow.com/questions/1536054/…
    – Hülya
    Nov 10 at 15:13










  • @nullpointer Because I'm using b in another class then the ser thing...
    – Stijn Bannink
    Nov 10 at 15:15














up vote
0
down vote

favorite












I want to convert my byte to a String, and then convert that String to a byte.



So,



byte b = myFunction();
String bstring = b.toString();
/* Here the methode to convert the bstring to byte, and call it ser */
String deser = new String(ser);


bstring gives me [B@74e752bb.



And then convert the String to byte. I'm not using it in this order, but this is an example.



How do I need to do this in Java?



Kind regards,



Stijn










share|improve this question



















  • 1




    why not new String(b) and what about bstring.getBytes()?
    – nullpointer
    Nov 10 at 15:09










  • Take a look at here: stackoverflow.com/questions/1536054/…
    – Hülya
    Nov 10 at 15:13










  • @nullpointer Because I'm using b in another class then the ser thing...
    – Stijn Bannink
    Nov 10 at 15:15












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to convert my byte to a String, and then convert that String to a byte.



So,



byte b = myFunction();
String bstring = b.toString();
/* Here the methode to convert the bstring to byte, and call it ser */
String deser = new String(ser);


bstring gives me [B@74e752bb.



And then convert the String to byte. I'm not using it in this order, but this is an example.



How do I need to do this in Java?



Kind regards,



Stijn










share|improve this question















I want to convert my byte to a String, and then convert that String to a byte.



So,



byte b = myFunction();
String bstring = b.toString();
/* Here the methode to convert the bstring to byte, and call it ser */
String deser = new String(ser);


bstring gives me [B@74e752bb.



And then convert the String to byte. I'm not using it in this order, but this is an example.



How do I need to do this in Java?



Kind regards,



Stijn







java arrays byte






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:25

























asked Nov 10 at 15:07









Stijn Bannink

165




165







  • 1




    why not new String(b) and what about bstring.getBytes()?
    – nullpointer
    Nov 10 at 15:09










  • Take a look at here: stackoverflow.com/questions/1536054/…
    – Hülya
    Nov 10 at 15:13










  • @nullpointer Because I'm using b in another class then the ser thing...
    – Stijn Bannink
    Nov 10 at 15:15












  • 1




    why not new String(b) and what about bstring.getBytes()?
    – nullpointer
    Nov 10 at 15:09










  • Take a look at here: stackoverflow.com/questions/1536054/…
    – Hülya
    Nov 10 at 15:13










  • @nullpointer Because I'm using b in another class then the ser thing...
    – Stijn Bannink
    Nov 10 at 15:15







1




1




why not new String(b) and what about bstring.getBytes()?
– nullpointer
Nov 10 at 15:09




why not new String(b) and what about bstring.getBytes()?
– nullpointer
Nov 10 at 15:09












Take a look at here: stackoverflow.com/questions/1536054/…
– Hülya
Nov 10 at 15:13




Take a look at here: stackoverflow.com/questions/1536054/…
– Hülya
Nov 10 at 15:13












@nullpointer Because I'm using b in another class then the ser thing...
– Stijn Bannink
Nov 10 at 15:15




@nullpointer Because I'm using b in another class then the ser thing...
– Stijn Bannink
Nov 10 at 15:15












3 Answers
3






active

oldest

votes

















up vote
2
down vote













When converting byte to String, you should use this,



new String(b, "UTF-8");


instead of,



b.toString();


When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.



Your method when dealt appropriately, should be written something like this,



 public static void main(String args) throws Exception 
byte b = myFunction();
// String bstring = b.toString(); // don't do this
String bstring = new String(b, "UTF-8");
byte ser = bstring.getBytes("UTF-8");
/* Here the methode to convert the bstring to byte, and call it ser */
String deser = new String(ser, "UTF-8");






share|improve this answer




















  • I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
    – Stijn Bannink
    Nov 10 at 15:24










  • If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
    – Pushpesh Kumar Rajwanshi
    Nov 10 at 15:25











  • Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
    – Stijn Bannink
    Nov 10 at 15:29











  • How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
    – Pushpesh Kumar Rajwanshi
    Nov 10 at 15:38


















up vote
1
down vote













You can do it like this,



String string = "Your String";
byte bytesFromString = string.getBytes(); // get bytes from a String
String StringFromByteArray = new String(bytesFromString); // get the String from a byte array





share|improve this answer








New contributor




Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    down vote













    I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).






    share|improve this answer








    New contributor




    Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.

















      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',
      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%2f53240247%2fjava-converting-byte-string-to-byte-array%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote













      When converting byte to String, you should use this,



      new String(b, "UTF-8");


      instead of,



      b.toString();


      When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.



      Your method when dealt appropriately, should be written something like this,



       public static void main(String args) throws Exception 
      byte b = myFunction();
      // String bstring = b.toString(); // don't do this
      String bstring = new String(b, "UTF-8");
      byte ser = bstring.getBytes("UTF-8");
      /* Here the methode to convert the bstring to byte, and call it ser */
      String deser = new String(ser, "UTF-8");






      share|improve this answer




















      • I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
        – Stijn Bannink
        Nov 10 at 15:24










      • If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:25











      • Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
        – Stijn Bannink
        Nov 10 at 15:29











      • How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:38















      up vote
      2
      down vote













      When converting byte to String, you should use this,



      new String(b, "UTF-8");


      instead of,



      b.toString();


      When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.



      Your method when dealt appropriately, should be written something like this,



       public static void main(String args) throws Exception 
      byte b = myFunction();
      // String bstring = b.toString(); // don't do this
      String bstring = new String(b, "UTF-8");
      byte ser = bstring.getBytes("UTF-8");
      /* Here the methode to convert the bstring to byte, and call it ser */
      String deser = new String(ser, "UTF-8");






      share|improve this answer




















      • I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
        – Stijn Bannink
        Nov 10 at 15:24










      • If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:25











      • Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
        – Stijn Bannink
        Nov 10 at 15:29











      • How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:38













      up vote
      2
      down vote










      up vote
      2
      down vote









      When converting byte to String, you should use this,



      new String(b, "UTF-8");


      instead of,



      b.toString();


      When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.



      Your method when dealt appropriately, should be written something like this,



       public static void main(String args) throws Exception 
      byte b = myFunction();
      // String bstring = b.toString(); // don't do this
      String bstring = new String(b, "UTF-8");
      byte ser = bstring.getBytes("UTF-8");
      /* Here the methode to convert the bstring to byte, and call it ser */
      String deser = new String(ser, "UTF-8");






      share|improve this answer












      When converting byte to String, you should use this,



      new String(b, "UTF-8");


      instead of,



      b.toString();


      When you are converting byte array to String, you should always specify a character encoding and use the same encoding while converting back to byte array from String. Best is to use UTF-8 encoding as that is quite powerful and compact encoding and can represent over a million characters. If you don't specify a character encoding, then platform's default encoding may be used which may not be able to represent all characters properly when converted from byte array to String.



      Your method when dealt appropriately, should be written something like this,



       public static void main(String args) throws Exception 
      byte b = myFunction();
      // String bstring = b.toString(); // don't do this
      String bstring = new String(b, "UTF-8");
      byte ser = bstring.getBytes("UTF-8");
      /* Here the methode to convert the bstring to byte, and call it ser */
      String deser = new String(ser, "UTF-8");







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 10 at 15:15









      Pushpesh Kumar Rajwanshi

      2,2851719




      2,2851719











      • I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
        – Stijn Bannink
        Nov 10 at 15:24










      • If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:25











      • Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
        – Stijn Bannink
        Nov 10 at 15:29











      • How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:38

















      • I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
        – Stijn Bannink
        Nov 10 at 15:24










      • If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:25











      • Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
        – Stijn Bannink
        Nov 10 at 15:29











      • How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
        – Pushpesh Kumar Rajwanshi
        Nov 10 at 15:38
















      I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
      – Stijn Bannink
      Nov 10 at 15:24




      I just want to have a save way to hash a string to save it in a yaml file, and that I can decrypt it.
      – Stijn Bannink
      Nov 10 at 15:24












      If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
      – Pushpesh Kumar Rajwanshi
      Nov 10 at 15:25





      If you want to save byte as it is, in some yaml file, you can use FileOutputStream to write bytes into a file. I am not sure what you mean by "save way to hash" Can you elaborate it a bit? Converting byte to String using b.toString() may not give you a particular string.
      – Pushpesh Kumar Rajwanshi
      Nov 10 at 15:25













      Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
      – Stijn Bannink
      Nov 10 at 15:29





      Sorry, I mean this: It works, but I want to save the byte on a safe way in a yaml file, and now I'm saving only the old string.
      – Stijn Bannink
      Nov 10 at 15:29













      How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
      – Pushpesh Kumar Rajwanshi
      Nov 10 at 15:38





      How are you saving it in safe way in yaml file? See if you can use base64 to just encode your byte array, as that will give you physical printable string version of your byte array.
      – Pushpesh Kumar Rajwanshi
      Nov 10 at 15:38













      up vote
      1
      down vote













      You can do it like this,



      String string = "Your String";
      byte bytesFromString = string.getBytes(); // get bytes from a String
      String StringFromByteArray = new String(bytesFromString); // get the String from a byte array





      share|improve this answer








      New contributor




      Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















        up vote
        1
        down vote













        You can do it like this,



        String string = "Your String";
        byte bytesFromString = string.getBytes(); // get bytes from a String
        String StringFromByteArray = new String(bytesFromString); // get the String from a byte array





        share|improve this answer








        New contributor




        Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.



















          up vote
          1
          down vote










          up vote
          1
          down vote









          You can do it like this,



          String string = "Your String";
          byte bytesFromString = string.getBytes(); // get bytes from a String
          String StringFromByteArray = new String(bytesFromString); // get the String from a byte array





          share|improve this answer








          New contributor




          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          You can do it like this,



          String string = "Your String";
          byte bytesFromString = string.getBytes(); // get bytes from a String
          String StringFromByteArray = new String(bytesFromString); // get the String from a byte array






          share|improve this answer








          New contributor




          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 10 at 15:17









          Sand

          4398




          4398




          New contributor




          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Sand is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















              up vote
              0
              down vote













              I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).






              share|improve this answer








              New contributor




              Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.





















                up vote
                0
                down vote













                I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).






                share|improve this answer








                New contributor




                Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.



















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).






                  share|improve this answer








                  New contributor




                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  I am no expert, but you should try the methods provided by the "Byte" class and if necessary, some loops. Try byte b = Byte.parseByte(String s) to convert a string to a byte and String s = Byte.toString(byte b) to convert a byte to a string. Hope this helps :).







                  share|improve this answer








                  New contributor




                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 10 at 15:16









                  Rakirnd

                  713




                  713




                  New contributor




                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Rakirnd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240247%2fjava-converting-byte-string-to-byte-array%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







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3