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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:53:10+00:00 2026-05-29T20:53:10+00:00

I did simple test and it seems that Java conversions of String to bytes[]

  • 0

I did simple test and it seems that Java conversions of String to bytes[] is not one-one, at least using UTF-8.
The code :

    byte[] bytes1 = {-1, 127, 0, 38, 97, 104, 55, 110, 50, -24, -48, 59, -20, -6, 64, 1, 4, 107, 56, 54 };      
    String msg  = new String( bytes1, "UTF-8" );        
    byte[] bytes2 = msg.getBytes( "UTF-8" );                            
    for( byte curr : bytes1 ) {         
        System.out.print( curr );
        System.out.print( ", " );
    }
    System.out.println();
    for( byte curr : bytes2 ) { 
        System.out.print( curr );
        System.out.print( ", " );
    }

I supposed that I’ll see two equals lines of output. In reality it was:

 -1, 127, 0, 38, 97, 104, 55, 110, 50, -24, -48, 59, -20, -6, 64, 1, 4, 107, 56, 54, 

 -17, -65, -67, 127, 0, 38, 97, 104, 55, 110, 50, -17, -65, -67, -17, -65, -67, 59, -17, -65, -67, -17, -65, -67, 64, 1, 4, 107, 56, 54, 

I wonder why it happens and how I can achieve one-one conversion. Anybody knows?

  • 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-29T20:53:12+00:00Added an answer on May 29, 2026 at 8:53 pm

    You cannot for arbitrary text. Conversion from UTF-16 (the representation in a String) to UTF-8 is defined to be not-one-to-one. See the Unicode standard at Unicode.org.

    It looks like what you really want is to pass “UTF-16” as the charset, thus asking for a byte serialization of UTF-16 instead of a conversion to UTF-8.

    See http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html. If you don’t want a BOM, use an ‘unmarked’ variation.

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

Sidebar

Related Questions

I am writing a simple FTP client using C#. I did not find any
I did a simple test to understand how transactions work in .net Sample code
After being troubled by an issue that I simply did not have the knowledge
Did you ever had a bug in your code, you could not resolve? I
I am testing performance on integer addition in Java. The way I did that
I use simple shell script to test some http server that process POST requests.
I was playing around with recursion and did this simple function. I was assuming
Ok I know this is simple, in fact damn simple, I did this in
Did you ever use Oracle auditing features on a production db? How did that
I am working on a project that involves me using a HashSet of a

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.