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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:02:09+00:00 2026-05-22T23:02:09+00:00

I am trying to write a simple program for this interview question: Write a

  • 0

I am trying to write a simple program for this interview question:

Write a function that checks for valid unicode byte sequence. A unicode
sequence is encoded as: – first byte indicates number of subsequent bytes
‘11110000’ means 4 subsequent data bytes – data bytes start with a
’10xxxxxx’

   public static void main(String[] args)
{

        System.out.println(checkUnicode(new byte[] {(byte)'c'}));

}

    /**
     * Write a function that checks for valid unicode byte sequence. A unicode
     * sequence is encoded as: - first byte indicates number of subsequent bytes
     * '1111000' means 4 subsequent data bytes - data bytes start with a
     * '10xxxxxx'
     * 
     * @param unicodeChar
     * @return
     */
 public static boolean checkUnicode(byte[] unicodeChar)
{
    byte b = unicodeChar[0];
    int len = 0;

    int temp = (int)b<<1;
    while((int)temp<<1 == 0)
    {
        len++;
    }
    System.out.println(len);

    if (unicodeChar.length == len) 
    {
        for(int i = 1 ; i < len; i++)
        {
            // Check if Most significant 2 bits in the byte are '10'
            // c0, in base 16, is 11000000 in binary
            // 10000000, in base 2, is 128 in decimal
            if( ( (int)unicodeChar[i]&0Xc0 )==128 )
            {
                continue;
            }
            else
            {
                return false;
            }
        }
        return true;
    }
    else
    {
        return false;
    }
}

The output I get is   
99
false  

Changed the conversion from char to byte array based on Chris Jester-Young’s comment.

Can someone point me to right direction

Thanks

Made some modifications based on input from Ted Hopp.
P.S:
I got the question from some forum and I think it wasn’t posted in correctly there, however I still decided to solve it and use it as is to prevent obfuscating it more, since I did not understand it completely either !

  • 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-22T23:02:10+00:00Added an answer on May 22, 2026 at 11:02 pm

    Here’s an enterprise level solution for your enterprise level job:

    public static void main(String[] args) {
        if (args.length == 0 || args[0] == null || (args[0] = args[0].trim()).isEmpty()) {
            System.out.println("No argument passed or argument empty!");
            return;
        }
    
        String arg = args[0];
        System.out.println("arg: " + arg + ", arg len: " + arg.length());
    
        BitSet bs = new BitSet(arg.length());
        for (int i = 0; i < arg.length(); i++) {
            if (arg.charAt(i) == '1') {
                bs.set(i, true); 
            }
        }
        ByteBuffer bb = ByteBuffer.wrap(bs.toByteArray());
        Charset cs = Charset.forName("UTF-8");
        CharsetDecoder csd =
                cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).
                onUnmappableCharacter(CodingErrorAction.REPORT)
                ;
    
        try {
            CharBuffer cb = csd.decode(bb);
            String uns = cb.toString();
            System.out.println("Got unicode string of len " + uns.length() + ": " + uns + " from " + arg + " -- no errors!");
        } catch (CharacterCodingException cce) {
            System.out.println("Invalid UTF-8 unicode string! " + cce.getMessage());
        }
    }
    

    Verification:

    public static void test() {
        StringBuilder sb = new StringBuilder();
         byte[] byt = new String("stupid interview").getBytes();
         BitSet byt1 = fromByteArray(byt);
         for (int i = 0; i < byt1.size(); i++) {
             sb.append(byt1.get(i) ? "1" : "0");
         }
         String[] st = new String[1];
         st[0] = sb.toString();
         main(st);
    }
    
    public static BitSet fromByteArray(byte[] bytes) {
        BitSet bits = new BitSet();
        for (int i=0; i<bytes.length*8; i++) {
            if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
                bits.set(i);
            }
        }
        return bits;
    }
    

    Output:

    11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110
    arg: 11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110, arg len: 128
    {0, 1, 4, 5, 6, 10, 12, 13, 14, 16, 18, 20, 21, 22, 28, 29, 30, 32, 35, 37, 38, 42, 45, 46, 53, 56, 59, 61, 62, 65, 66, 67, 69, 70, 74, 76, 77, 78, 80, 82, 85, 86, 89, 92, 93, 94, 97, 98, 100, 101, 102, 104, 107, 109, 110, 112, 114, 117, 118, 120, 121, 122, 124, 125, 126}
    Got unicode string of len 16: stupid interview from 11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110 -- no errors!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple high-low program that will output like this: $
I am trying to write a really simple Outlook VSTO add in that checks
I'm trying to write a simple program that will compare the files in separate
I am trying to write a simple program that lets me overlay a dot
Im trying to write a simple sample program which checks for any new mail
I'm trying to write a very simple program in C++ that finds the modulus
I am trying to write this simple image modifier program in c++ with opencv
I'm trying to write a simple program in VC++ which will just initialize the
I'm just starting out writing trying to write a simple program in C and
I'm trying to write a simple WPF app that has two ellipses, joined by

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.