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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:43:39+00:00 2026-05-23T10:43:39+00:00

I am trying to read an image and use Base64 encoding to convert it

  • 0

I am trying to read an image and use Base64 encoding to convert it into byte array and then to string to send it over network. The problem is that when I try to decode the Base64 encoded string, I am getting incorrect data.

For eg. I am facing issue with below special character.

I am using following code for encoding:

byte[] b = Base64.encodeBase64(IOUtils.toByteArray(loInputStream));
String ab = new String(b);

IOUtils is org.apache.commons.io.IOUtils.

and loInput

Code for decoding:

byte[] c = Base64.decodeBase64(ab.getBytes());
String ca = new String(c);
System.out.println(ca);

It prints ? for decoded String.

Can anyone please let me know the issue.

  • 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-23T10:43:39+00:00Added an answer on May 23, 2026 at 10:43 am

    As I’ve said elsewhere, in Java, String is for text, and byte[] is for binary data.

    String ≠ byte[]

    Text ≠ Binary Data

    An image is binary data. Base64 is an encoding which allows transmission of binary data over US_ASCII compatible text channels (there is a similar encoding for supersets of ASCII text: Quoted Printable).

    So, it goes like:

    Image (binary data) → Image (text, Base64 encoded binary data) → Image (binary data)

    where you would use String encodeBase64String(byte[]) to encode, and byte[] decode(String) to decode. These are the only sane API’s for Base64, byte[] encodeBase64(byte[]) is misleading, the result is US_ASCII-compatible text (so, a String, not byte[]).

    Now, text has a charset and an encoding, String uses a fixed Unicode/UTF-16 charset/encoding combination internally, and you have to specify a charset/encoding when converting something from/to a String, either explicitly, or implicitly, using the platform’s default encoding (which is what PrintStream.println() does). Base64 text is pure US_ASCII, so you need to use that, or a superset of US_ASCII. org.apache.commons.codec.binary.Base64 uses UTF8, which is a superset of US_ASCII, so all is well. (OTOH, the internal java.util.prefs.Base64 uses the platform’s default encoding, so I guess it would break if you start your JVM with, say, an UTF-16 encoding).

    Back on topic: you’ve tried to print the decoded image (binary data) as text, which obviously hasn’t worked. PrintStream has write() methods that can write binary data, so you could use those, and you would get the same garbage as if you wrote the original image. It would be much better to use a FileOutputStream, and compare the resulting file with the original image file.

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

Sidebar

Related Questions

I'm trying to convert a bitmap image into an uncompressed tif file for use
From PostgreSQL, am trying to read an image file into an Imagick object -
I am trying to use the boost generic image library to convert CYMK images
I'm trying to use the command line program convert to take a PDF into
I'm trying to read in an image URL. As mentioned in the java documentation,
I'm trying to read a .doc file into a database so that I can
I'm trying to use ServletContext.getResource to retrieve a java.net.url reference to an image file
I am trying to read an image that resides somewhere on the web from
I am trying to read a TIFF image to perform processing. The ideal would
Im trying to read in image file from a server , with the code

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.