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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:06:27+00:00 2026-05-28T19:06:27+00:00

I’m trying to send and receive a byte stream in which certain ranges of

  • 0

I’m trying to send and receive a byte stream in which certain ranges of bytes represent different pieces of data. I’ve found ways to convert single primitive datatypes into bytes, but I’m wondering if there’s a straightforward way to place certain pieces of data into specified byte regions.

For example, I might need to produce or read something like the following:

byte 1 - int
byte 2-5 - int
byte 6-13 - double
byte 14-21 - double
byte 25 - int
byte 26-45 - string

Any suggestions would be appreciated.

  • 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-28T19:06:28+00:00Added an answer on May 28, 2026 at 7:06 pm

    Try DataOutputStream/DataInputStream or, for arrays, the ByteBuffer class.

    For storing the integer in X bytes, you may use the following method. If you think it is badly named, you may use the much less descriptive i2os name which is used in several (crypto) algorithm descriptions. Note that the returned octet string uses Big Endian encoding of unsigned ints, which you should specify for your protocol.

    public static byte[] possitiveIntegerToOctetString(
            final long value, final int octets) {
        if (value < 0) {
            throw new IllegalArgumentException("Cannot encode negative values");
        }
    
        if (octets < 1) {
            throw new IllegalArgumentException("Cannot encode a number in negative or zero octets");
        }
    
        final int longSizeBytes = Long.SIZE / Byte.SIZE;
        final int byteBufferSize = Math.max(octets, longSizeBytes);
        final ByteBuffer buf = ByteBuffer.allocate(byteBufferSize);
        for (int i = 0; i < byteBufferSize - longSizeBytes; i++) {
            buf.put((byte) 0x00);
        }
        buf.mark();
        buf.putLong(value);
    
        // more bytes than long encoding
        if (octets >= longSizeBytes) {
            return buf.array();
        }
    
        // less bytes than long encoding (reset to mark first)
        buf.reset();
        for (int i = 0; i < longSizeBytes - octets; i++) {
            if (buf.get() != 0x00) {
                throw new IllegalArgumentException("Value does not fit in " + octets +  " octet(s)");
            }
        }
    
        final byte[] result = new byte[octets];
        buf.get(result);
        return result;
    }
    

    EDIT before storing the string, think of a padding mechanism (spaces would be most used), and character-encoding e.g. String.getBytes(Charset.forName("ASCII")) or "Latin-1". Those are the most common encodings with a single byte per character. Calculating the size of “UTF-8” is slightly more difficult (encode first, add 0x20 valued bytes at the end using ByteBuffer).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.