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

  • Home
  • SEARCH
  • 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 8251655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:18:27+00:00 2026-06-08T00:18:27+00:00

I’m developing an Android 2.3.3 application with Java. This app is an iOS code

  • 0

I’m developing an Android 2.3.3 application with Java.

This app is an iOS code with unsigned data types port to Java.

On iOS it works with UInt16 and UInt8. In one case instead using byte[] I’m using char[].

But know I have to send that char[] as a byte[] using a DatagramPacket.

If one element of char[] is 128, how can I do to insert into byte[] and the receiver gets 128. I don’t know what happens if I do this:

char c = (char)128;
byte b = (byte)c;

Which will be b value?
128 = 10000000. b = -1 or b = 127?

How can I convert char[] to byte[] without losing any bits?

  • 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-08T00:18:29+00:00Added an answer on June 8, 2026 at 12:18 am

    In Java char is an unsigned 16-bit quantity. So you can directly convert your uint16 to char without doing anything else.

    For unsigned 8-bit quantity you have 2 options:

    1. Use byte. It also holds 8 bits. You don’t lose any bits just because it is signed. However, if you do arithmetic with it you need to remember that Java will scale byte up automatically to an int and sign-extend it. To prevent this just always mask it like this:

      byte b;
      int foo = 5 * (b & 0xFF);

    2. Use char. It is unsigned and can hold 16 bits so the 8 bits will fit in there quite nicely. To put a byte into a char just do this:

      byte b;
      char c = (char)(b & 0xFF); // Mask off the low-order 8 bits

    To put a char into a byte just do:

    char c;
    byte b = (byte)c; // Truncates to 8 bits
    

    Be aware that byte in Java is signed, so that whenever you do arithmetic with it you need to mask the low-order 8 bits only (to prevent sign-extension). Like this:

    byte b;
    int foo = (b & 0xFF);
    

    You can do all the normal bitwise operations you want with a byte without having to mask:

    byte b;
    if (b & 0x80) ... // Test a flag
    b |= 0x40; // Set a flag
    b ^= 0x20; // Flip a flag from 0 to 1 or 1 to 0
    b ^= ~0x10; // Clear a flag
    byte x = b << 3; // Shift left 3 bits and assign
    byte x = b >>> 4; // Shift right 4 bits and assign (WITHOUT sign extension)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I

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.