Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,907 questions

51,839 answers

573 users

How to convert byte array to StringBuilder in Java

1 Answer

0 votes
public class Main {
    public static StringBuilder ByteArrayToCharArray(byte barr[]) {
        StringBuilder sb = new StringBuilder();
        int len = barr.length;
         
        for (int i = 0; i < len; i++) {
            if (barr[i] >= 32 && barr[i] != 92 && barr[i] != 127) {
                sb.append((char)barr[i]);
            }
            else {
                String temp;
                if (barr[i] == 92) {
                    sb.append("\\\\");
                }
                else {
                    temp = String.format("\\0x%02x", barr[i]);
                    sb.append(temp);
                }
            }
        }
         
        return sb;
    }
 
    public static void main(String[] args) {
        byte barr[] = {97, 98, 99, 100, 127};
         
        StringBuilder sb = ByteArrayToCharArray(barr);
 
        System.out.println(sb);
    }
}
 
 
 
/*
run:

abcd\0x7f
 
*/

 



answered Dec 21, 2024 by avibootz
edited Dec 21, 2024 by avibootz

Related questions

1 answer 87 views
1 answer 167 views
2 answers 191 views
4 answers 188 views
2 answers 166 views
2 answers 352 views
1 answer 138 views
...