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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:23:21+00:00 2026-06-12T13:23:21+00:00

I need to write this in Java. Although I know how to read a

  • 0

I need to write this in Java.

Although I know how to read a file, I’m interested in what the ‘buff’ output is, is the length at the beginning?

char              *buff;
unsigned char     *aux;


while(fgets (buff+2, length, fin)){
    len = strlen (buff + 2) + 2;
    aux = (unsigned char *) &len;
    buff[1] = aux[0];
    buff[0] = aux[1];
    ...
    send (sd, buff, len, 0);

}

but I don’t understand this:

aux = (unsigned char *) &len;
buff[1] = aux[0];
buff[0] = aux[1];

Thanks in advance.

  • 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-12T13:23:22+00:00Added an answer on June 12, 2026 at 1:23 pm

    This code:

    aux = (unsigned char *) &len;
    buff[1] = aux[0];
    buff[0] = aux[1];
    

    creates a byte-array view of the integer value stored in len. In Java, you can’t do exactly the same thing, but you can get what you want a couple of ways. The easiest is just with bit masking and shifting:

    int len = ...;
    byte[] buff = ...;
    byte[0] = (byte) (len & 0xff);
    byte[1] = (byte) ((len >> 8) & 0xff);
    

    Another possibility is to use a ByteBuffer, but that seems overkill for such a simple operation.

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

Sidebar

Related Questions

Problem: I need to write/read objects from a file.This because I need to write/read
I need to write a Java web applet (if this can't be done via
I need to write some code in Java to upload a file(.tz format) to
I need to write a bash script to compile my java program. I know
I'm pretty new to Java but I need to write something like this C#
I need to write a java script. This is supposed to validate if the
I need to write this program using a loop https://i.stack.imgur.com/gko6T.jpg this is what i
I need to write a macro that operates on bitarray looking like this: array[0]
I have this Structure the elements of which that I need to write in
Maybe I am just over thinking this and need to write some more prototype

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.