-2

This morning I try to basically convert a byteArray to String and reconvert it to an byteArray but I notice I got some extraByte, so I conclude I can't to this basic stuff.

Someone can explain me why ? Because basically a String is an sequence of byte so an byteArray, right ?

I need to convert my byteArray to a String to store it in the dataStore (androidStudio) and back it to byteArray when I need to use it... If someone got a solution to do that

token.accessTokenCipher = Pair<ByteArray, ByteArray>
println("RAW ${token.accessTokenCipher.first.size} - AccessCipher : "+token.accessTokenCipher.first)
println("RAW ${token.accessTokenCipher.second.size} - AccessIV : "+token.accessTokenCipher.second)
println("Decrypt : "+Token.getAccessTokenCipher(Pair(token.accessTokenCipher.first,token.accessTokenCipher.second)))
val a = token.accessTokenCipher.first.toString(Charsets.UTF_8)
val q = token.accessTokenCipher.second.toString(Charsets.UTF_8)
println("STR - AccessCipher : "+a)
println("STR - AccessIV : "+q)
val p = a.toByteArray(Charsets.UTF_8)
val o = q.toByteArray(Charsets.UTF_8)
println("BA ${p.size} - AccessCipher : "+p)
println("BA ${o.size} - AccessIV : "+o)
println("Decrypt : "+Token.getAccessTokenCipher(Pair(p,o))))
Raphael M
  • 109
  • 1
  • 9
  • Have you considered that you might use different or unsupported encoding for the content? – Martheen Oct 21 '20 at 09:36
  • 2
    "String is an sequence of byte" - not really. A String is a sequence of characters. For the mapping from bytes to characters and back there are different encodings, which may or may not be fully bidirectional – Thomas Kläger Oct 21 '20 at 09:37
  • Please post some code that reproduces your issue. There are more chances of you making a mistake somewhere, than that a thing that has been constantly supported and updated for 25+ years. – AlexT Oct 21 '20 at 09:52
  • @Alex.T it's do – Raphael M Oct 21 '20 at 09:56
  • 2
    You don't use string encoding unless you *start* from text. Here, you start from *bytes*, so you should use Base64 instead. – Martheen Oct 21 '20 at 10:35
  • 1
    Your byte array has content that is not valid UTF-8. Invalid bytes get replaced when using the string constructor. When converting the result to UTF-8, you get a valid UTF-8 sequence containing UTF-8 representations of the replacement character. You could use ISO-8859-1 aka ISO-LATIN-1 instead, which has the property that every byte value maps to a character, so converting back and forth is possible. However, if you plan to transport it as text, you risk data loss, as not ever character is guaranteed to be safely transported. Then, Base64 is the better choice. – Holger Oct 21 '20 at 17:52

1 Answers1

0

small example how to handle

public class ByteArrays {
    public static void main(String[] args) {
        String myString = "This is a nice String :D";
        byte[] bytesOfmyString = myString.getBytes();
        System.out.println(Arrays.toString(bytesOfmyString)); // -> [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 110, 105, 99, 101, 32, 83, 116, 114, 105, 110, 103, 32, 58, 68]
        System.out.println(new String(bytesOfmyString)); // -> This is a nice String :D
        
    }
}