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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:01:38+00:00 2026-06-13T03:01:38+00:00

I have a byte array that consists of a 10 byte message. I know

  • 0

I have a byte array that consists of a 10 byte message. I know the first 2 bytes contains an id that would like to extract out to a string. An example of this id is: 2001. The remaining bytes consist of other fields which I am aware.

I am receiving the binary data from a UDP message and after attempting to convert the binary/ byte array with the methods below, my print still logs a binary message.

The code I am using is as follows to convert the binary UDP message:

 DatagramPacket rcvMsg = receivepacket.getData();
 String id = new String(rcvMsg, 0, 2, "US-ASCII");
 System.out.println("id ----- : "+id);

How can I get this to print 2001 from the binary datagram message?

  • 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-13T03:01:39+00:00Added an answer on June 13, 2026 at 3:01 am

    In what format do you want recover the first 2 bytes? Do they represent an integer, or are they an encoded string?

    Because, otherwise you could wrap your array into ByteArrayInputStream, and this latter into a DataInputStream, and then read your message as you consider appropriate, by using the DataInputStream methods.

    DataInput input = new DataInputStream(new ByteArrayInputStream(myArray));
    String id = String.valueOf(input.readShort())
    

    You could even continue to use this object to read the rest of the fields encoded in the array if they correspond to supported Java data types (i.e. byte, short, int, String, etc).

    Now, if your first two bytes represent just ASCII characters (or any other encoding), then you are good to go with one of the String constructors:

    String id = new String(myBytes, 0, 2, "US-ASCII");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SHA-1 byte array that I would like to use in a
I have a byte array that contains list of unicode strings. Each string is
I have a byte array that contains 6 bytes last 2 represents the port
If I have a byte array that contains UTF8 content, how would I go
I have byte array as input. I would like to convert that array to
I have a byte array that may or may not have null bytes at
I have a byte that is an array of 30 bytes, but when I
I have a byte array that I'm encoding to a string: Private Function GetKey()
I have a byte array in java. That array contains '%' symbol somewhere in
I have a byte array that contains a jpeg image. I was just wondering

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.