Skip to content

Hex encoding fails to generate leading zeros #11

Closed

Description

@quintesse
p.setProperty("test","الألبانية");
p.store(System.out);

Expected output: test=\u0627\u0644\u0623\u0644\u0628\u0627\u0646\u064a\u0629
Actual output: test=\u627\u644\u623\u644\u628\u627\u646\u64a\u629

Activity

  1. wilinz commented on Jul 28, 2022

    @wilinz

    I've opened a new pull request, can you see?

  2. wilinz commented on Jul 28, 2022

    @wilinz

    Here is my solution

    private String encodeUnicode(String raw) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < raw.length(); i++) {
                char c = raw.charAt(i);
                if (c > 0x7F) { //Escape non-ascii characters
                    String hex = Integer.toHexString(c);
                    if (hex.length() < 4) {
                        hex = String.format("%4s", hex).replace(" ", "0");
                    }
                    builder.append("\\u").append(hex);
                } else {
                    builder.append(c);
                }
            }
            return builder.toString();
        }
  3. wilinz commented on Jul 28, 2022

    @wilinz

    0x7F-0xFF should also be encoded, even if it is in the encoding range of ISO8859-1, otherwise sometimes the editor uses ASCII or utf-8 decoding to cause garbled characters

  4. added a commit that references this issue on Jul 28, 2022
    95cd6c9
  5. quintesse commented on Jul 28, 2022

    @quintesse
    CollaboratorAuthor

    0x7F-0xFF should also be encoded, even if it is in the encoding range of ISO8859-1, otherwise sometimes the editor uses ASCII or utf-8 decoding to cause garbled characters

    I can understand that it can be a nuisance dealing with those characters in an editor, but I'm not sure I want to make that change. It might make your life easier but for a large percentage of the world who use ISO8859-1 suddenly their files would change when loading and then saving again. I don't think that's acceptable. It would also change the behaviour of org.codejive.properties.Properties with respect to java.util.Properties.

  6. added a commit that references this issue on Jul 28, 2022
    bf4bdd9
  7. quintesse commented on Jul 28, 2022

    @quintesse
    CollaboratorAuthor

    Ok, so I merged a PR that fixes the issue of not having 4 hex digits... but.... I'm rethinking how to handle UTF8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions