views:

1574

answers:

5

I'd like to write a simple detail formatter that displays byte[] data in the form of a String (using String.<init>([B) to do the dirty work).

However, I'm not sure how to find the class name for [B to use when creating the formatter. Is this even possible? Or, alternatively, is there another way to view byte arrays as strings in the debugger?

+1  A: 
byte[].class
Michael Borgwardt
That tells me the class, but doesn't allow me to format the byte array as a string.Any idea how to get from here to there?
Chris R
A: 

If your question is how would I get a string representation of a byte array of [0,1,2] to be "[0,1,2]", I would suggest you take a look at Arrays.toString(byte[])

http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#toString(byte[])

whaley
Nope, I want to interpret the bytes as UTF-8 string data.
Chris R
More specifically, I want to use Eclipse's detail formatter to do it.
Chris R
A: 

The [B type can be retrieved with byte[].class.

Nathaniel Flath
+3  A: 

I don't know how to get eclipse's detail formatter to automagically display byte arrays as Strings, but you can display a particular byte array by adding new String(byteArray) as a watch expression.

Jason Day
+1 you can't display a byte array as a string, but your method is the closest thing to what the question is looking for
Yuval A
Take care with character encoding. The `new String(bytes)` will use platform default character encoding which may not per se be the correct character encoding.
BalusC
A: 

You couldn't add Detail Formatters for simple Datatypes like byte

Thorsten