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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:27:47+00:00 2026-05-29T21:27:47+00:00

I have an array result that contains values from 0-255. I originally declared it

  • 0

I have an array “result” that contains values from 0-255. I originally declared it as byte[], but when I have to write 128, result[i] gets value -128 and in the output file it is written “€” that is finally read as 8364.

As I can see that byte only accepts values -128 to 127, what data type should I use for values from 0-255 (without wasting memory)?

Should I change as well Content Type or add any charset header?

Thanks

res.setContentType("application/octet-stream"); 
res.setHeader("Content-Disposition","attachment;filename=output.js");
ServletOutputStream os = res.getOutputStream();
byte[] result=encode(req.getParameter("originalScript")); // Result[i]=-128 (should be 128)
os.write(result,0,result.length); // result[i] on output.js is written as "€" (8364)
  • 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-29T21:27:48+00:00Added an answer on May 29, 2026 at 9:27 pm

    You’re confused by mixing several concepts.

    First of all, the int 128 is the same as the byte -128 (int 255 == byte -1, 254 == -2, … 128 = -128). Bytes are signed and the sign information is in the highest bit. Your mistake here is that you didn’t use the correct way to convert the byte value back to an int. To fix this, use this code:

    b = (byte) 128;
    int i = b & 0xff;
    System.out.println(b);
    System.out.println(i);
    

    gives -128 and 128.

    Next: ASCII is only defined for values between 0 and 127. This means anything > 127 is garbage unless you treat it carefully.

    The problem is when you read the output of your code. Since ASCII can’t contain values > 127, what should the reading code do?

    “output.js” sounds like you’re using a web browser to read this data as a JavaScript file. The web browser will try to convert the byte stream into text using an “encoding”. You don’t specify one, the browser has to make a guess and gets it wrong (and application/octet-stream seems wrong, too. Shouldn’t that be text/javascript?).

    You have two options:

    1. Change encode() to return a properly encoded UTF-8 string (UTF-8 is a way to send unicode as bytes) and set the charset to UTF-8 (which is usually the default but better be safe than sorry):

      response.setHeader("Content-Type", "text/javascript; charset=UTF-8");
      
    2. Set the charset to ISO-8859-1 which will preserve the bytes 1:1. This will fail if your script contains any Unicode characters > 255. Since there won’t be an error, you should not use this approach. I just mention it for completeness.

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

Sidebar

Related Questions

I have an array DetailsArray in VB.NET which contains the result of a LINQ
I have byte array as input. I would like to convert that array to
I have a web service that when invoked, returns a Result object that contains
Let's say I have an Array that contains more than three repetitions of a
What I have is 2 hash's one gets dumped from a backend that deals
I want to remove rows from my array so that my result is an
I have an array which has the contents as the result of an sql
I have an array I've created in JavaScript. The end result comes out to
Hey guys I have an assigned array from mysql results and I simply want
Let's say I have array of bytes: byte[] arr = new byte[] { 0,

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.