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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:35:07+00:00 2026-05-26T10:35:07+00:00

I have a requirement to send data from native program written in C through

  • 0

I have a requirement to send data from native program written in C through Sockets, namely the winsock interface. That data is then received by a java program. The Java program receives a packet of data of which it know the order and makes some processing. It takes the int received from the socket.Read() like client_socket.getInputStream().read()

A processing of that int data returned from the Read() is parsed to get the data types expected. Basically it is a slicing of the bulk data received. I can only assume that the read() functions reads 4 bytes at a time (the 32 bit java int size). So I proceed to separate that 4 bytes(8 bits not the Java type) into 4 Java shorts so i can correctly represent the unsigned values they represent.
After i have the 4 shorts if i know i want eg a uint16, i just concatenate the 2 shorts

The problem is somewhere i am doing some wrong bit manipulation that is not working out as i thought it would.
The C code and the Java code is below and its really simple even though it does not work. The output is something which i can’t understand why it is the way it is.

0 0   0 0    0 1     0 0     0 2     0 0     0 3     0 0     0 4 {...}

The C code redacted the initialization part:

uint16_t buffer[255] = {};
uint16_t *current_pointer = buffer;
for(uint16_t index = 0; index < 255; index++) {
    *current_pointer = index;
    current_pointer++;
}
Write(client_socket, (char *)buffer, sizeof(buffer));

The java code also redacted:

public final short[] JavaIntToUint8Array(int unsigned_int) {
    return new short[] { (short)((unsigned_int & 0xFF000000L) >> 24),
                (short)((unsigned_int & 0x00FF0000L) >> 16),
                (short)((unsigned_int & 0x0000FF00L) >> 8),
                (short)((unsigned_int & 0x000000FFL))};
}
public final int[] JavaIntToUint16(int unsigned_int) {
    short uint8_array[] = JavaIntToUint8Array(unsigned_int);
    return new int[] { (int)(uint8_array[0] << 8) | (int)uint8_array[1],            
        (int)(uint8_array[2] << 8) | (int)(uint8_array[3]) };
}

…

while (index < 255) {
        read_data = data_input.read();
        System.out.print(" ");
        System.out.print(JavaIntToUint16(read_data)[0]);
        System.out.print(" ");
        System.out.print(JavaIntToUint16(read_data)[1]);
        System.out.print("\t");
        index++;

}
  • 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-26T10:35:08+00:00Added an answer on May 26, 2026 at 10:35 am

    The simplest approach to reading unsigned short numbers is to use DataInput.readUnsignedShort().

    DataInputStream dis = new DataInputStream(data_input);
    int num = dis.readUnsignedShort();
    

    This uses big-endian or network endian. If you are using little endian (e.g. on an x86/x64 processor) you can change the byte order yourself or using ByteBuffer to do it.

    // using NIO
    SocketChannel sc = SocketChannel.open(new InetSocketAddress("localhost", 12345));
    ByteBuffer bb = ByteBuffer.allocateDirect(32*1024).order(ByteBuffer.LITTE_ENDIAN));
    while(sc.read(bb) >= 0) {
        bb.flip();
        while(bb.remaining() > 1) {
           int num = bb.getShort() & 0xFFFF;
        }
        bb.compact();
    }
    

    Do you really need to send a stream of unsigned short values? A more often used stream is a unsigned bytes and these are simpler.

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

Sidebar

Related Questions

I have a requirement where I need to send some data from my server
I have a requirement to send some 100 bytes data over internet .My machine
In our project we have requirement that, after receiving sms message from third party
We have the requirement to take a form submission and save some data, then
I have a requirement to export some data from a table (not all fields
I have a require ment to read data from a table(SQL 2005) and send
I have this requirement from client where depending on the location of the iPhone
The requirement of the TCP server: receive from each client and send result back
I have a requirement to parse a huge text file and send parts of
I have a requirement in my application that I think can be met by

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.