Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Conversion from byte[] to char[]

 
Ranch Hand
Posts: 495
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello i have a byte array and i am trying to convert this to a char array, Can anyone offer suggestions please
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do these bytes represent? How did you get it?

I think that a String could be the intermediate way:
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this:

Byte[] byte = new Byte[3]; // byte array
byte[0] = 1;
byte[1] = 2;
byte[2] = 3;

StringBuilder buffer = new StringBuilder();
for(int i;i < byte.length();i++){
buffer.append(byte[i]);
}

//get char array
char[] ch = buffer.toString().toCharArray();
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not directly do

byte[] bytes = new byte[3]; // byte array
bytes[0] = 1;
bytes[1] = 2;
bytes[2] = 3;

char[] convertedChar = new char[bytes.length];
for(int i=0;i < bytes.length;i++){
convertedChar[i]=(char)bytes[i];
}
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that byte array contains text in a certain character encoding, and you want to make characters out of it.

Michael's solution converts the byte values to numeric strings. So if the bytes would have the values 65, 66, 67, the string would become "656667". Is that what you want?

Vikram's solution directly casts the bytes to chars. This assumes that the bytes are perhaps ASCII characters. If the bytes would contain 65, 66, 67, you would get "ABC".

If you know what character encoding the text in the bytes is in, then you can pass that to the constructor of class String to do the conversion correctly. For example:
 
Jiuxing Liu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,Jesper's reply is good!

we should do that:

byte[] bytes = ...;
String str = new String(bytes); //using the platform's default charset
Char[] chars = str.toCharArray();
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, credit should also go to Rob Prime who posted the same thing above!
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic