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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:42:25+00:00 2026-06-12T14:42:25+00:00

I have a byte array and create string from it using new String(array) .

  • 0

I have a byte array and create string from it using new String(array). When I convert it back to a byte array using .getBytes(), it doesn’t give me the original byte array back. What gives?

String text = "two hats";
boolean t1 = Arrays.equals(text.getBytes(), text); // true

byte[] barray = {(byte)0x8f, (byte)0xd5, (byte)0xaf, (byte)0x30, (byte)0xb9};        

String test1 = new String(barray); 
boolean t2 = Arrays.equals(barray.getBytes(), test1); // false

// I tried setting an encoding but that didn't help.

Charset cs = Charset.forName("UTF-8"); 
String test2 = new String(barray, cs);       
boolean t3 = Arrays.equals(barray, test2, cs); // false

Here’s the code I am actually using.

// test byte array vs string
public static void testEqual(byte[] bytes, String str) {
    byte[] fromString = str.getBytes();        

    printBytes(bytes);        
    printBytes(fromString);        
    System.out.println(Arrays.equals(bytes, fromString));        
}

// test byte array vs string, with charset
public static void testEqual(byte[] bytes, String str, Charset charset) {
    byte[] fromString = str.getBytes(charset);        

    printBytes(bytes);        
    printBytes(fromString);        
    System.out.println(Arrays.equals(bytes, fromString));
}

// prints bytes as hex string
public static void printBytes(byte[] bytes) {
    for (byte b: bytes) {
        System.out.print(String.format("%02X ", b));
    }        
    System.out.println();
}

public static void main(String[] args) {
    String text = "two hats";
    testEqual(text.getBytes(), text); // works fine

    byte[] barray = {(byte)0x8f, (byte)0xd5, (byte)0xaf, (byte)0x30, (byte)0xb9};        

    String test1 = new String(barray); // breaks      
    testEqual(barray, test1);

    Charset cs = Charset.forName("UTF-8"); // breaks too
    String test2 = new String(barray, cs);       
    testEqual(barray, test2, cs);
}

Demo: http://ideone.com/IRHlb

PS: I don’t want to use Base64 or somesuch

  • 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-06-12T14:42:26+00:00Added an answer on June 12, 2026 at 2:42 pm

    You appear to be trying to store arbitrary binary data by converting it to a string using the platform default encoding. Don’t do that. Use base64 or hex to represent arbitrary binary data as text. There are lots of classes for base64 conversion; I like this public domain one.

    If the data really is a binary-encoded form of some text, you should specify that encoding explicitly – but this is only appropriate if the original data was text. (Using the platform default encoding is almost always a bad idea.)

    Binary data and text data are very different. Converting opaque binary data into a string arbitrarily is like expecting to be able to load an arbitrary file into an image editor and see something useful.

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

Sidebar

Related Questions

I have a String created from a byte[] array, using UTF-8 encoding. However, it
I am trying to create a md5 string from the byte array of an
I have been writing some code to create a byte array I will be
I have a byte array and I need to convert this to a image.
I have a byte array, received from java application via network. I need to
I have a byte array filled from a file uploaded. But, in another part
I have a byte array of size 4 byte[] source = new byte[4]; Now
How do I convert a structure to a byte array in C#? I have
I have a byte array which needs to be marshalled into the following struct:
I have a byte array highlighted below, how do I insert it into 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.