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

  • Home
  • SEARCH
  • 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 380995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:02:33+00:00 2026-05-12T15:02:33+00:00

Having ignored it all this time, I am currently forcing myself to learn more

  • 0

Having ignored it all this time, I am currently forcing myself to learn more about unicode in Java. There is an exercise I need to do about converting a UTF-16 string to 8-bit ASCII. Can someone please enlighten me how to do this in Java? I understand that you can’t represent all possible unicode values in ASCII, so in this case I want a code which exceeds 0xFF to be merely added anyway (bad data should also just be added silently).

Thanks!

  • 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-12T15:02:34+00:00Added an answer on May 12, 2026 at 3:02 pm

    How about this:

    String input = ... // my UTF-16 string
    StringBuilder sb = new StringBuilder(input.length());
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        if (ch <= 0xFF) {
            sb.append(ch);
        }
    }
    
    byte[] ascii = sb.toString().getBytes("ISO-8859-1"); // aka LATIN-1
    

    This is probably not the most efficient way to do this conversion for large strings since we copy the characters twice. However, it has the advantage of being straightforward.

    BTW, strictly speaking there is no such character set as 8-bit ASCII. ASCII is a 7-bit character set. LATIN-1 is the nearest thing there is to an “8-bit ASCII” character set (and block 0 of Unicode is equivalent to LATIN-1) so I’ll assume that’s what you mean.

    EDIT: in the light of the update to the question, the solution is even simpler:

    String input = ... // my UTF-16 string
    byte[] ascii = new byte[input.length()];
    for (int i = 0; i < input.length(); i++) {
        ascii[i] = (byte) input.charAt(i);
    }
    

    This solution is more efficient. Since we now know how many bytes to expect, we can preallocate the byte array and in copy the (truncated) characters without using a StringBuilder as intermediate buffer.

    However, I’m not convinced that dealing with bad data in this way is sensible.

    EDIT 2: there is one more obscure “gotcha” with this. Unicode actually defines code points (characters) to be “roughly 21 bit” values … 0x000000 to 0x10FFFF … and uses surrogates to represent codes > 0x00FFFF. In other words, a Unicode codepoint > 0x00FFFF is actually represented in UTF-16 as two “characters”. Neither my answer or any of the others take account of this (admittedly esoteric) point. In fact, dealing with codepoints > 0x00FFFF in Java is rather tricky in general. This stems from the fact that ‘char’ is a 16 bit type and String is defined in terms of ‘char’.

    EDIT 3: maybe a more sensible solution for dealing with unexpected characters that don’t convert to ASCII is to replace them with the standard replacement character:

    String input = ... // my UTF-16 string
    byte[] ascii = new byte[input.length()];
    for (int i = 0; i < input.length(); i++) {
        char ch = input.charAt(i);
        ascii[i] = (ch <= 0xFF) ? (byte) ch : (byte) '?';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 233k
  • Answers 233k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would recommend using the Rename feature to place the… May 13, 2026 at 5:49 am
  • Editorial Team
    Editorial Team added an answer You can't return at compile time (preproc const) something you… May 13, 2026 at 5:49 am
  • Editorial Team
    Editorial Team added an answer It depends whether you want a join model or not.… May 13, 2026 at 5:49 am

Related Questions

I recently switched to J2EE from .NET, and am confused about where to put
I believe this is not the popular stance but I prefer embedded hacks such
I'm using jQuery 1.3.2. I'm having trouble getting a correct height in Internet Explorer
I am doing the below test to try and learn more about LINQ to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.