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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:48:26+00:00 2026-05-25T06:48:26+00:00

My program is receiving an integer array from a browser application that’s interpreted as

  • 0

My program is receiving an integer array from a browser application that’s interpreted as UTF-8 (example in code). I can echo my resulting string (“theString” shown in the code below) back to the browser and everything’s fine. But it’s not fine in the Java program. The input string is “Hällo”. But it prints out from the Java program as “Hõllo”.

import java.io.*;
import java.nio.charset.*;

public class TestCode {
   public static void main (String[] args) throws IOException {

      // H : 72
      // ä : 195 164
      // l : 108
      // o : 111
      // the following is the input sent from browser representing String = "Hällo"
      int[] utf8Array = {72, 195, 164, 108, 108, 111};

      String notYet = new String(utf8Array, 0, utf8Array.length);
      String theString = new String(notYet.getBytes(), Charset.forName("UTF-8"));

      System.out.println(theString);
   }
}
  • 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-25T06:48:27+00:00Added an answer on May 25, 2026 at 6:48 am

    This will do the trick:

    int[] utf8Array = {72, 195, 164, 108, 108, 111};
    byte[] bytes = new byte[utf8Array.length];
    for (int i = 0; i < utf8Array.length; ++i) {
        bytes[i] = (byte) utf8Array[i];
    }
    String theString = new String(bytes, Charset.forName("UTF-8"));
    

    The problem with passing int[] directly is that the String class interprets every int as a separate char, while after converting to byte[] String treats input as raw bytes and understands that 195, 164 is actually is a single character consisting of two bytes rather than two characters.

    UPDATE: Answering your comment, unfortunately, Java is that verbose. Compare it to Scala:

    val ints = Array(72, 195, 164, 108, 108, 111)
    println(new String(ints map (_.toByte), "UTF-8"))
    

    Once again the difference between int and byte is not just the compiler being picky, they really mean different things when it comes to UTF-8 encoding.

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

Sidebar

Related Questions

I am receiving an error that says The application was unable to start correctly
The following program shows that we can use return or pthread_exit to return a
I'm trying to parse a datestamp (that I got from Twitter) but am receiving
My Python program is receiving ICMP destination unreachable messages from a raw socket. The
How to design a C/C++ program so that it can save some data after
In my program, I have been receiving an error when I use a command-line
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
What program can I use to decompile a class file? Will I actually get
The program that I am currently assigned to has a requirement that I copy
A program that I work on assumes that the UUID generated by the Windows

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.