Hi Guys, I'm trying to find a quick, easy way of converting Java Primitives to Byte Arrays. For example, a 'long' primitive contains 8 bytes...so I want to convert/cast this to a 'byte'. In simple terms...the equivilent of: String.getBytes() but for java primitives. Any ideas? Thanks in advance. -Nick
I really don't think that's what the original poster wanted. If what you want is the equvalent of the bytes used in memory to store the value, then there are two approaches I can think of now. One approach is the traditional shift-and-mask technique: the lowest byte is (val & 0xff), the next byte is ((val>>8) & 0xff) etc. Another approach is to use one of the OutputStreams which writes the values as bytes (DataOutputStream ?) but capture it to an array ByteArrayOutputStream. Is this more like what you are after?