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 7512069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:45:03+00:00 2026-05-29T23:45:03+00:00

Given the following code: String tmp = new String(\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a); String result = convertToEffectiveString(tmp); //

  • 0

Given the following code:

String tmp = new String("\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a");

String result = convertToEffectiveString(tmp); // result contain now "hello\n"

Does the JDK already provide some classes for doing this ?
Is there a libray that does this ? (preferably under maven)

I have tried with ByteArrayOutputStream with no success.

  • 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-29T23:45:06+00:00Added an answer on May 29, 2026 at 11:45 pm

    This works, but only with ASCII. If you use unicode characters outside of the ASCCI range, then you will have problems (as each character is being stuffed into a byte, instead of a full word that is allowed by UTF-8). You can do the typecast below because you know that the UTF-8 will not overflow one byte if you guaranteed that the input is basically ASCII (as you mention in your comments).

    package sample;
    
    import java.io.UnsupportedEncodingException;
    
    public class UnicodeSample {
        public static final int HEXADECIMAL = 16;
    
        public static void main(String[] args) {
    
            try {
                String str = "\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a";
    
                String arr[] = str.replaceAll("\\\\u"," ").trim().split(" ");
                byte[] utf8 = new byte[arr.length];
    
                int index=0;
                for (String ch : arr) {
                    utf8[index++] = (byte)Integer.parseInt(ch,HEXADECIMAL);
                }
    
                String newStr = new String(utf8, "UTF-8");
                System.out.println(newStr);
    
            }
            catch (UnsupportedEncodingException e) {
                // handle the UTF-8 conversion exception
            }
        }
    }
    

    Here is another solution that fixes the issue of only working with ASCII characters. This will work with any unicode characters in the UTF-8 range instead of ASCII only in the first 8-bits of the range. Thanks to deceze for the questions. You made me think more about the problem and solution.

    package sample;
    
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;
    
    public class UnicodeSample {
        public static final int HEXADECIMAL = 16;
    
        public static void main(String[] args) {
    
            try {
                String str = "\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a\\u3fff\\uf34c";
    
                ArrayList<Byte> arrList = new ArrayList<Byte>();
                String codes[] = str.replaceAll("\\\\u"," ").trim().split(" ");
    
                for (String c : codes) {
    
                    int code = Integer.parseInt(c,HEXADECIMAL);
                    byte[] bytes = intToByteArray(code);
    
                    for (byte b : bytes) {
                        if (b != 0) arrList.add(b);
                    }
                }
    
                byte[] utf8 = new byte[arrList.size()];
                for (int i=0; i<arrList.size(); i++) utf8[i] = arrList.get(i);
    
                str = new String(utf8, "UTF-8");
                System.out.println(str);
            }
            catch (UnsupportedEncodingException e) {
                // handle the exception when
            }
        }
    
        // Takes a 4 byte integer and and extracts each byte
        public static final byte[] intToByteArray(int value) {
            return new byte[] {
                    (byte) (value >>> 24),
                    (byte) (value >>> 16),
                    (byte) (value >>> 8),
                    (byte) (value)
            };
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code: using (var client = new WebClient()) { string url =
given the following code? final Map<String, List<E>> map = new HashMap<String, List<E>>(); List<E> list
Given the following code: var myList = new List<string> { red, blue, green };
Given the following code: var myList = new List<string> { red shirt, blue, green,
I tried the following code in LINQPad and got the results given below: List<string>
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code: string istanbul = 523; Convert.ToInt32(istanbul.ToString(00)); what does it return?
Given the following code : public abstract class Participant { private String fullName; public
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
Given the following code... class Program { static void Main(string[] args) { Foo foo

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.