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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:11:17+00:00 2026-05-23T07:11:17+00:00

final byte LOGIN_REQUEST = 1; long deviceId = 123456789; String nickname = testid; Socket

  • 0
  final byte LOGIN_REQUEST = 1;
  long deviceId = 123456789;
  String nickname = "testid";

  Socket mSocket = new Socket("localhost", 12021);
  ByteBuffer bBuffer = ByteBuffer.allocate(1);
  bBuffer.order(ByteOrder.LITTLE_ENDIAN);

  //1
  bBuffer.put(LOGIN_REQUEST);

  //8
  bBuffer.putLong(deviceId);

  byte[] bString = nickname.getBytes();
  int sLength = bString.length;

  //4
  bBuffer.putInt(sLength);
  bBuffer.put(bString);

I am sending byte data like this and I want to parse it on my linux server using c++

In c++, I am reading

 char *pdata = new char[BUF_SIZE];
 int dataLength = read(m_events[i].data.fd, pdata, BUF_SIZE);

and push the pdata into the pthread’s queue. I think I have to read first byte to see the type of the packet and read the next 8byte to get the device id and so on..
Please give me some references or tutorial to do this in c++ code..

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-05-23T07:11:18+00:00Added an answer on May 23, 2026 at 7:11 am

    The below code will effectively do the trick. I am assuming a java int is 32bit

    #include <inttypes.h>
    
    // Declare variables
    unsigned char login_req;
    int64_t       device_id;
    uint32_t      name_len;
    char*         name_str;
    
    // Populate variables;
    login_req = pdata[0];
    memcpy( &device_id, pdata+1, 8 );
    memcpy( &name_len, pdata+9, 4 );
    name_str = (char*)malloc( name_len + 1 );
    memcpy( name_str, pdata+13, name_len );
    name_str[name_len] = '\0';
    

    Note: I am glossing over some stuff, namely

    • Does not handle when BUF_SIZE is to small
    • Does not handle when the C program machine is not little ENDIAN. If it is big endian then you would need to switch the bytes after the memcpy for device_id and name_len
    • Does not to type cast on memcpy calls to avoid possible compiler warnings

    This solution is pure C, will work in C++ too

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

Sidebar

Related Questions

public final byte[] getParam(String commandName,String memLocation,String dataId){ byte[] result = new byte[9]; result[0] =
public final byte[] getParam(String commandName,String memLocation,String dataId){ byte[] result = new byte[9]; //GET_PARAM, RAM,WATER_OUTLET_TEMP
I have a snippet of code creating a new String as follow: private final
public static Logger getLogger() { final Throwable t = new Throwable(); final StackTraceElement methodCaller
public class TrippleENCRSPDESCSP { public TrippleENCRSPDESCSP() { } public void EncryptIt(string sData,ref byte[] sEncData,ref
Here are my constants //Encryption fields /** Algorithm=RSA Mode=ECB Padding=PKCS1Padding*/ public static final String
public static final String readURL(String url)throws Throwable { try { InputStream in = (InputStream)
I am trying to generate a key for encryption: public static final String ENCYT_ALGORITHM
public final class MyScreen extends MainScreen { /** * Creates a new MyScreen object
Final question for the night. And apologies for the complete noobness of this. 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.