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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:45:31+00:00 2026-06-17T01:45:31+00:00

I have stream of bytes, which I got by using getInputStream() method from socket.

  • 0

I have stream of bytes, which I got by using getInputStream() method from socket. How to read 1 or 2 bytes from this stream with offset n and convert them to integer.
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-06-17T01:45:32+00:00Added an answer on June 17, 2026 at 1:45 am

    You can try to use DataInputStream which allows you to read primitive types:

    DataInputStream dis = new DataInputStream(...your inputStream...);
    int x = dis.readInt();
    

    UPD: More specifically, you could use the code of readInt() method:

        int ch1 = in.read();
        int ch2 = in.read();
        int ch3 = in.read();
        int ch4 = in.read();
        if ((ch1 | ch2 | ch3 | ch4) < 0)
            throw new EOFException();
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
    


    UPD-2: If you read 2-bytes array and sure, that it contains full integer number, try this:

        int value = (b2[1] << 8) + (b2[0] << 0)
    

    UPD-3: Pff, full method for doing it:

    public static int read2BytesInt(InputStream in, int offset) throws IOException {
    
        byte[] b2 = new byte[2];
        in.skip(offset);
        in.read(b2);
    
        return (b2[0] << 8) + (b2[1] << 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stream of bytes that is being read in from a socket
I have a stream of bytes which contains a flag which identifies the endianness
using BouncyCastle and with help from a stackoverflow question I got this: using System.Net.Sockets;
I have a stream of bytes (given by a BufferedInputStream) which represents a sequence
I have a WCF service which has one method returning a stream. [ServiceContract] public
I have a stream of bytes which actually (if put right) will form a
I have a Stream of audio data coming from my mic for which I
I have a little web server application which reads X bytes from a file
I have a specific requirement to convert a stream of bytes into a character
I have response stream from a ftp web request that returns binary file. 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.