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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:37:41+00:00 2026-05-25T17:37:41+00:00

I need some help converting a java byte array to a 7-Bit ASCII string.

  • 0

I need some help converting a java byte array to a 7-Bit ASCII string. However I am getting 8-bit sequences and need to escape any unreadable character to it’s escaped sequence. Is there a simple solution for this or do I need to build my own?

Seeing that the range of readable characters in 7-bit ASCII is continuous right now I am thinking of the following:

for( int i = 0; i < buffer.length; i++ ) {
   int codePoint = ( (int) buffer[ i ] ) & 255;
   if( 0x20 <= codePoint && codePoint <= 0x7e ) {
      res = res + String( (char) codePoint );
   } else {
     String c = Integer.toHexString( codePoint );
     if( c.length() < 2 ) {
       c = "0" + c;
     }
     res = res + "\\0x" + c;
   }
}

However this seems like an awful lot of work for such a simple conversion. Is there a better way?

Also I might need to do the same to data that has been converted from the byte array to strings. Is there a simpler solution in this case?

  • 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-25T17:37:42+00:00Added an answer on May 25, 2026 at 5:37 pm
      public static String escape(byte[] data) {
        StringBuilder cbuf = new StringBuilder();
        for (byte b : data) {
          if (b >= 0x20 && b <= 0x7e) {
            cbuf.append((char) b);
          } else {
            cbuf.append(String.format("\\0x%02x", b & 0xFF));
          }
        }
        return cbuf.toString();
      }
    

    You can use the format method to pare back the verbiage.

    Note that this method is only safe because the ASCII range matches the lower range of the UTF-16 encoding used by Java Strings.

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

Sidebar

Related Questions

I need a little help converting some VB.NET code to C#. I have tried
I need some help converting an SQL query into relational algebra. Here is the
I need some help converting the SQL found below to an equivalent LINQ statement
I need some help in converting my script from using mysql to mysqli I
I need some help with a program for converting Fahrenheit to Celsius in C.
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with

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.