// Sign is 3 (bigger than 0)
// Factor is -2 (move decimal point 2 positions right)
// Digits are: 123
HiveDecimalObjectInspector boi = (HiveDecimalObjectInspector) poi;
HiveDecimal dec = boi.getPrimitiveJavaObject(o);
// get the sign of the big decimal
int sign = dec.compareTo(HiveDecimal.ZERO);
// we'll encode the absolute value (sign is separate)
dec = dec.abs();
// get the scale factor to turn big decimal into a decimal < 1
int factor = dec.precision() - dec.scale();
factor = sign == 1 ? factor : -factor;
// convert the absolute big decimal to string
dec.scaleByPowerOfTen(Math.abs(dec.scale()));
String digits = dec.unscaledValue().toString();
// finally write out the pieces (sign, scale, digits)
buffer.write((byte) ( sign + 1), invert);
buffer.write((byte) ((factor >> 24) ^ 0x80), invert);
buffer.write((byte) ( factor >> 16), invert);