简体   繁体   中英

Android & Java: String.getBytes() not parsing escaped characters properly

I have a Java String "Hi\\n" that I would like to convert to a byte[] , but when I have a variable whose content is "Hi\\n" and I call variable.getBytes(StandardCharsets.UTF_8) the \\ and n were parsed separately rather than together as a newline character, so I end up with 4 bytes in the array rather than the expected 3 bytes.

How can I perform this conversion correctly?

Edit: Screenshot added. I set a breakpoint where I'm calling getBytes() from, and evaluated the expression when payload is "hi\\n" , and the evaluation returned a byte[] of 4 bytes. When I do "hi\\n".getBytes(StandardCharsets.UTF_8) directly however I get 3 bytes, which confuses me.

在此处输入图片说明

Rookie mistake on my part. The problem was because the "Hi\\n" example was a user-inputted String coming from an Android EditText instead of a real String literal, so the backslash \\ was already escaped as \\\\ and so the actual content of that String was ["H", "i", "\\\\", "n"] . I added some code to do input sanitization:

// Assume payload is content of an EditText which the user has entered "Hi\n" in
payloadInBytes = payload.replace("\\n", "\n").getBytes(StandardCharsets.UTF_8);

After this payloadInBytes is 3 bytes long as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM