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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:48:55+00:00 2026-06-04T09:48:55+00:00

public static byte[] objectToByteArray(Object obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream

  • 0
public static byte[] objectToByteArray(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream objOut = null;
try {
    objOut = new ObjectOutputStream(out);
    objOut.writeObject(obj);
    objOut.flush();
} finally {
    objOut.close();
    out.close();
}
return out.toByteArray();
}

Main Method :



 public static void main(String[] args) throws IOException {
//
// System.out.println(getFileName("/home/local/ZOHOCORP/bharathi-1397/logs/bharathi-1397.csez.zohocorpin.com_2012_05_24.log",0));
try {
    throw new IOException("Error in main method");
} catch (IOException io) {
    System.out.println(new String(objectToByteArray(io.getMessage()),
        "UTF-8"));
}
//
}

Output :
��

I want to convert Object to byte[] but why it returns ctrl characters like this . I didn’t understand can you please help me .

  • 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-04T09:48:56+00:00Added an answer on June 4, 2026 at 9:48 am

    Serialization converts an object into binary data. It’s fundamentally not text data – just like an image file isn’t text data.

    Your code tries to interpret this opaque binary data as if it were UTF-8-encoded text data. It’s not, so no wonder you’re seeing garbage. If you opened up an image file in a text editor, you’d see similar non-useful text. You can try to interpret the data as text (as you are doing) but you won’t get anything useful out.

    If you want to represent opaque binary data as text in a printable, reversible manner you should use base64 or hex. There are lots of libraries available to convert to base64, including this public domain one.

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

Sidebar

Related Questions

public static byte[] Compress(byte[] data) { using (MemoryStream ms = new MemoryStream()) { using
public static byte[] main_Mem = new byte[2048]; public static SlotNode[] cache = new SlotNode[8];
public class URLReader { public static byte[] read(String from, String to, String string){ try
public static byte[] sendRequest(String url ,Object params ,boolean isHttps){ if(isHttps){ HttpsURLConnection urlConnection = (HttpsURLConnection)newURL(url).openConnection();
I have the following function: public static byte[] StringToByte(string str) { int length =
I am trying to encrypt some data with the following code: public static byte[]
public class b { public static void main(String[] args) { byte b = 1;
I have picked up this example which converts BitSet to Byte array. public static
For obtaining client IP in WCF i use the following method: public static byte[]
I have the following code to serialize /deserialize a DataTable: public static byte[] Serialize(DataTable

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.