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 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

Related Questions

I am having a heck of a time with this one guys. I am
I'm always having troubles with svn:ignore, but this time I think I may be
I'm having a hard time figuring this out but how do I tell my
I am kinda new to all this but I am trying to make myself
I am having an issue where padding top is being ignored in Safari in
This is an annoying problem I am having with Twisted.web. Basically, I have a
I am having trouble implementing a python algorithm which does the following: (This is
(there is a follow up to this question here ) I am working on
I'm having a hard time trying to remove duplicated images from an specific text/article.
I think I am having a problem with the scope of 'this' when needing

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.