Hey I'm working on an app that uses Paho mqtt
Now I'm trying to cast the contents of a couple of objects to byte arrays so I can send them to the broker. There are a couple of different objects that all adhere to a abstract class, but the one I started with contains a double[]
Here's the function I'm trying to implement:
@Override
public byte[] getBytes() {
return Arrays.stream(driveVector).map(d -> Double.valueOf(d).byteValue()).toArray();
}
I thought this would work, but I get an error that the return value is a double[]
I think I either don't understand the map method or I'm goin about this all wrong in general (I looked at the ByteBuffer class, but it seems like a pain to implement this with it)
Thanks in advance
doubleto a singlebyte, or to the 8 bytes that make it up?mapoperation will yield aDoubleStreamand therefore you're getting back adouble[]when you calltoArray()forloop of copying values fromdouble[]tobyte[]would do it, and run much faster too.mapmethod ofDoubleStreammaps to anotherDoubleStream. You can usemapToInt,mapToLong, ormapToObjif you want a different type of stream, but there is nomapToByte.Double.valueOf(d).byteValue(): simply use(byte) d, to avoid the unnecessary boxing.