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
java arrays byte
add a comment |
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
java arrays byte
1
why notnew String(b)
and what aboutbstring.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
add a comment |
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
java arrays byte
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
java arrays byte
edited Nov 10 at 15:25
asked Nov 10 at 15:07
Stijn Bannink
165
165
1
why notnew String(b)
and what aboutbstring.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
add a comment |
1
why notnew String(b)
and what aboutbstring.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
add a comment |
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");
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
add a comment |
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
New contributor
add a comment |
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 :).
New contributor
add a comment |
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");
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
add a comment |
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");
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
add a comment |
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");
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");
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
add a comment |
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
add a comment |
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
New contributor
New contributor
answered Nov 10 at 15:17
Sand
4398
4398
New contributor
New contributor
add a comment |
add a comment |
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 :).
New contributor
add a comment |
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 :).
New contributor
add a comment |
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 :).
New contributor
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 :).
New contributor
New contributor
answered Nov 10 at 15:16
Rakirnd
713
713
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
why not
new String(b)
and what aboutbstring.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