Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6238715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:15:47+00:00 2026-05-24T11:15:47+00:00

How to get encoded version of string (e.g. \u0421\u043b\u0443\u0436\u0435\u0431\u043d\u0430\u044f) using Java? EDIT: I guess

  • 0

How to get encoded version of string (e.g. \u0421\u043b\u0443\u0436\u0435\u0431\u043d\u0430\u044f) using Java?

EDIT:
I guess the question is not very clear… Basically what I want is this:

Given string s=”blalbla” I want to get string “\uXXX\uYYYY”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:15:48+00:00Added an answer on May 24, 2026 at 11:15 am

    You will need to extract each code point/unit from the String and encode it yourself. The following works for all Strings even if the individual linguistic characters within the String are composed of digraphs or ligatures.

    public String getUnicodeEscapes(String aString)
    {
        if (aString != null && aString.length() > 0)
        {
            int length = aString.length();
            StringBuilder buffer = new StringBuilder(length);
            for (int ctr = 0; ctr < length; ctr++)
            {
                char codeUnit = aString.charAt(ctr);
                String hexString = Integer.toHexString(codeUnit);
                String padAmount = "0000".substring(hexString.length());
                buffer.append("\\u");
                buffer.append(padAmount);
                buffer.append(hexString);
            }
            return buffer.toString();
        }
        else
        {
            return null;
        }
    }
    

    The above produces output as dictated by the Java Language Specification on Unicode escapes, i.e. it produces output of the form \uxxxx for each UTF-16 code unit. It addresses supplementary characters by producing a pair of code units represented as \uxxxx\uyyyy.

    The originally posted code has been modified to produce Unicode codepoints in the format U+FFFFF:

    public String getUnicodeCodepoints(String aString)
    {
        if (aString != null && aString.length() > 0)
        {
            int length = aString.length();
            StringBuilder buffer = new StringBuilder(length);
            for (int ctr = 0; ctr < length; ctr++)
            {
                char ch = aString.charAt(ctr);
                if (Character.isLowSurrogate(ch))
                {
                    continue;
                }
                else
                {
                    int codePoint = aString.codePointAt(ctr);
                    String hexString = Integer.toHexString(codePoint);
                    String zeroPad = Character.isHighSurrogate(ch) ? "00000" : "0000";
                    String padAmount = zeroPad.substring(hexString.length());
                    buffer.append(" U+");
                    buffer.append(padAmount);
                    buffer.append(hexString);
                }
            }
            return buffer.toString();
        }
        else
        {
            return null;
        }
    }
    

    The gruntwork is done by the String.codePointAt() method which returns the Unicode codepoint at a particular index. For a String instance composed of combinational characters, the length of the String instance will not be the length of the number of visible characters, but the number of actual Unicode codepoints. For example, क and ् combine to form क् in Devanagari, and the above function will rightfully return U+0915 U+094d without any fuss as String.length() will return 2 for the combined character. Strings with supplementary characters will be with single codepoints for the individual characters – (the page will not display this String literal correctly, but you can copy this just fine; it should be Javascript but written using the supplementary character set for Mathematical alphanumeric symbols) will return U+1d4a5 U+1d4b6 U+1d4cb U+1d4b6 U+1d4c8 U+1d4b8 U+1d4c7 U+1d4be U+1d4c5 U+1d4c9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following warning when using java.net.URLEncoder.encode : warning: [deprecation] encode(java.lang.String) in java.net.URLEncoder
How to get back from encoded byte[] to java.security.Key? import java.security.Key; import javax.crypto.SecretKey; import
Is it safe to pass raw base64 encoded strings via GET parameters?
I have the following case where I get the result of UTF-8 encoded HTTP
I have am trying to use sed to get some info that is encoded
I'm trying to URL encode a string to form a GET request from objective-c.
I am calling a WebService and get a string returned from a WebMethod. The
Why do I get this error, when I use the pretty print version? ''
So I am using simplexml_load_string on some posted XML. However I get the below
I have some code which is using this function and I get an error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.