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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:52:48+00:00 2026-05-28T02:52:48+00:00

How can I create a String object from a byte array byte arr[MAX_SIZE]; //

  • 0

How can I create a String object from a byte array

byte arr[MAX_SIZE];  // Java

where one of the array elements is a C null terminating byte? Is it as simple as calling

String str = new String( arr );

Will the String constructor know to automatically stop at the null terminating character? Any bytes after the null byte are (possibly) garbage characters that I don’t want to include in the string. The last response under Parsing byte array containg fields of unknown length suggests looping through the array and manually finding the null terminating character, but I was wondering whether the String constructor will do this automatically. I also assume the system’s default charset will be used on all ends.

  • 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-28T02:52:48+00:00Added an answer on May 28, 2026 at 2:52 am
    byte arr[] = ...
    Charset charset = ...
    
    // Find the position of the first zero byte
    int i;
    for (i = 0; i < arr.length && arr[i] != 0; i++) { }
    
    
    String str = new String(arr, 0, i, charSet);
    

    Notes:

    • It is generally a good idea to use an explicit CharSet parameter so that your application doesn’t depend on the platform’s default characterset / encoding.

    • This won’t work for some charsets. For instance, a UTF-16 encoded string can’t safely be represented as a zero-terminated byte sequence because many code units contain zero bytes. (On the other hand, UTF-8 is OK provided that the string contains no instances of code point zero; see Can UTF-8 contain zero byte?)

    … but I was wondering whether the String constructor will do this automatically.

    No it / they won’t. (Don’t "wonder" … read the javadoc :-))

    I also assume the system’s default charset will be used on all ends.

    If you don’t specify a charset, the Java platform’s default will be used. This is likely to be the system default, but that is not guaranteed.

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

Sidebar

Related Questions

In javascript you can create a Date object from a string, like var mydate
How can I create an empty one-dimensional string array?
In the following example i can create an object dynamically via a string; however,
Is there anyway I can create nodes out of a string? I've searched the
i want to create a string S , which can be used as an
If I have List<String> text how can I create a sub-list of all continious
I create a GUID (as a string) and get the hash of it. Can
I'm trying to create an array of strings that can be randomized and limited
I want to send a byte array from a web service to a client
What is the longest string that can be created in .NET? The docs for

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.