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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:13:24+00:00 2026-05-23T06:13:24+00:00

Hi all the code is as follows: File file2 = new File(D://deploy//body.txt); byte[] bytes

  • 0

Hi all the code is as follows:

File file2 = new File("D://deploy//body.txt");

byte[] bytes = loadFile(file2);
System.out.println(bytes.length);

StringBuffer buffer = new StringBuffer();
InputStream inputStream = new ByteArrayInputStream(bytes);
InputStreamReader reader = new InputStreamReader(inputStream,"CP1252");
Reader in = new BufferedReader(reader);
int ch;
while ((ch = in.read()) > -1) {
    buffer.append((char)ch);
}
in.close(); 
System.out.println(buffer.toString().getBytes().length);

The final result is 1576 and 2439 for the length of the byte arrays. What is a proper way of converting a CP1252 byte array to a string and retain the proper size? Thanks

  • 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-23T06:13:25+00:00Added an answer on May 23, 2026 at 6:13 am

    I noticed your phrase – “proper string”, and would like to point out that there is no such thing as a proper or improper string in your case. It’s the encoding that is either proper or improper.

    You’re reading the byte sequence of cp1252 bytes, and appending the individual characters into a buffer. If the original file is in cp1252, there are no problems with this process. Under the hood, the InputStreamReader employs a CharsetDecoder that is capable of decoding the underlying charset of the stream, into a sequence of sixteen-bit Unicode characters (UTF-16). This is done, because you are reading characters from the byte stream.

    As pointed out by bmargulies, when you execute buffer.toString().getBytes() you are transforming these sequences of UTF-16 characters into a byte sequence that has the same encoding as the platform. Since this is not cp1252, the lengths of the original byte array and the transformed one are not comparable. Specifying the charset to the getBytes() method causes a StringEncoder (this is an internal class with the Oracle/Sun JVM; other implementations might use a different class) to be used, to transform the UTF-16 character sequence to the sequence of bytes in the desired encoding (cp1252).

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

Sidebar

Related Questions

Hi all i have my XML file as follows Name of XML XMLFile2.xml <?xml
i am new to work on xml.i have used an xml file as follows:
The following is my simplified code <?php $database = new db(); $file = new
Hi all i have written a code to move a file from the original
I have a C project where all code is organized in *.c / *.h
I have a problem where I need to remove all code and triggers from
I frequently code numerous experiments to test various algorithms, libraries, or hardware. All code,
I'm trying to find a way to delay all code that takes place after
I am working on a solo project, writing all code myself so no collaboration
First, an example of something that works as expected: (all code was executed in

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.