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

  • Home
  • SEARCH
  • 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 6758309
In Process

The Archive Base Latest Questions

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

I receive from socket a string in a byte array which look like :

  • 0

I receive from socket a string in a byte array which look like :

[128,5,6,3,45,0,0,0,0,0]

The size given by the network protocol is the total lenght of the string (including zeros) so , in my exemple 10.

If i simply do :

String myString = new String(myBuffer); 

I have at the end of the string 5 non correct caracter. The conversion don’t seems to detect the end of string caracter (0).

To get the correct size and the correct string i do this :

int sizeLabelTmp = 0;
//Iterate over the 10 bit to get the real size of the string
for(int j = 0; j<(sizeLabel); j++) {
    byte charac = datasRec[j];
    if(charac == 0)
        break;
    sizeLabelTmp ++;
}
// Create a temp byte array to make a correct conversion
byte[] label    = new byte[sizeLabelTmp];
for(int j = 0; j<(sizeLabelTmp); j++) {
    label[j] = datasRec[j];
}
String myString = new String(label);

Is there a better way to handle the problem ?

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-05-26T13:46:08+00:00Added an answer on May 26, 2026 at 1:46 pm

    0 isn’t an “end of string character”. It’s just a byte. Whether or not it only comes at the end of the string depends on what encoding you’re using (and what the text can be). For example, if you used UTF-16, every other byte would be 0 for ASCII characters.

    If you’re sure that the first 0 indicates the end of the string, you can use something like the code you’ve given, but I’d rewrite it as:

    int size = 0;
    while (size < data.length)
    {
        if (data[size] == 0)
        {
            break;
        }
        size++;
    }
    
    // Specify the appropriate encoding as the last argument
    String myString = new String(data, 0, size, "UTF-8");
    

    I strongly recommend that you don’t just use the platform default encoding – it’s not portable, and may well not allow for all Unicode characters. However, you can’t just decide arbitrarily – you need to make sure that everything producing and consuming this data agrees on the encoding.

    If you’re in control of the protocol, it would be much better if you could introduce a length prefix before the string, to indicate how many bytes are in the encoded form. That way you’d be able to read exactly the right amount of data (without “over-reading”) and you’d be able to tell if the data was truncated for some reason.

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

Sidebar

Related Questions

I'm escaping all the string parameters I receive from a php form like this:
I'm writting a financial C# application which receive messages from the network, translate them
These days I'm facing a problem with encoding string from an array of byte.
I am writing a byte array from a socket client as: byte[] arr =
I have an array of bytes that I receive from an external entity. It
I am using thread which will receive messages from the external application.So my thread
I have a user interface in .net which needs to receive data from a
I receive a string from a database query, then I remove all HTML tags,
I am doing one project related to socket programming in which from server I
I've writen a function to send and receive message from server like this: //Send

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.