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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:17:51+00:00 2026-05-27T07:17:51+00:00

I am writing a piece of code in Java (I’m fairly new to Java)

  • 0

I am writing a piece of code in Java (I’m fairly new to Java) that I have previously written in C#. Here’s the code and the example in C#.

ushort number = 0xAABB; // 43707
byte[] data = new byte[2];
EndianBitConverter.Big.CopyBytes(number, data, 0); // value[0] = 170, value[1] = 187

I’m using custom bit convrter in .NET since it defaults to little endian. Anyway, from what I understand about java, if I want to use the same result as byte[] I should expect my values (170 and 187) to be smaller by 128 (Byte.MAX_VALUE + 1) that is (42, 59) – due to .net and java having different range for type byte.
Here’s what I wrote in Java to mimic my above logic.

public class Ushort {
    private int value = 0;

    public Ushort(int i) {
        value = i - (Short.MAX_VALUE + 1);
    }

    public int get() {
        return value;
    }

    public byte[] getBytes() {
        byte[] result = new byte[]{
                (byte) (value >>> 24),
                (byte) (value >>> 16),
                (byte) (value >>> 8),
                (byte) value};

        return new byte[]{result[2], result[3]};
    }
}

However when I call the above code with

new Ushort(0xAABB).getBytes()

The result is [42, -69] instead [42, 59]. The last byte is smaller by 128 than it should.
I really need some pointers on how to do this properly and if my logic is correct.
I also need to do the same for uint, ulong and so on, so I need to understand this correctly.

  • 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-27T07:17:52+00:00Added an answer on May 27, 2026 at 7:17 am

    Either I do not understand the reasons behind the conversions you are trying to do, or they are wrongly conceived, which means that I cannot opine as to whether there is an error in their implementation.

    The type byte in java is the exact same as the type sbyte in C#, so you can do all your testing in C# using sbyte and make sure things work correctly there before porting to java.

    (byte)0xaa = 170
    (sbyte)0xaa = -86
    (byte)0xbb = 187
    (sbyte)0xbb = -69
    

    So, in Java your byte array should be { -86, -69 }.

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

Sidebar

Related Questions

I am writing a piece of code in Java that needs to take a
I am writing a piece of Java code that needs to send mail to
I'm writing a piece of code that requires the DOM of a website to
I am writing a piece of code that to work would require an extensive
I recently found myself writing a piece of code that executed a Core Data
I was writing some Unit tests last week for a piece of code that
I am writing a piece of code in which i have to find only
I have a created text file in unix enviroment using java code. For writing
I am writing a piece of code in which i have to convert from
A newbie question. I have the following piece of Java code: import acm.program.*; import

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.