return ConvertBytes.longToCharBytes(((Long.MAX_VALUE + 1L) << 1) + longval);
return ConvertBytes.longToCharBytes(((Long.MAX_VALUE + 1L) << 1) + longval);
private static byte[] getUnsignedNegativeBytes(RubyInteger arg) { // calculation for negatives when %u specified // for values >= Integer.MIN_VALUE * 2, MRI uses (the equivalent of) // long neg_u = (((long)Integer.MAX_VALUE + 1) << 1) + val // for smaller values, BigInteger math is required to conform to MRI's // result. long longval; BigInteger bigval; if (arg instanceof RubyFixnum) { // relatively cheap test for 32-bit values longval = ((RubyFixnum)arg).getLongValue(); if (longval >= Long.MIN_VALUE << 1) { return ConvertBytes.longToCharBytes(((Long.MAX_VALUE + 1L) << 1) + longval); } // no such luck... bigval = BigInteger.valueOf(longval); } else { bigval = ((RubyBignum)arg).getValue(); } // ok, now it gets expensive... int shift = 0; // go through negated powers of 32 until we find one small enough for (BigInteger minus = BIG_MINUS_64 ; bigval.compareTo(minus) < 0 ; minus = minus.shiftLeft(32), shift++) {} // add to the corresponding positive power of 32 for the result. // meaningful? no. conformant? yes. I just write the code... BigInteger nPower32 = shift > 0 ? BIG_64.shiftLeft(32 * shift) : BIG_64; return stringToBytes(nPower32.add(bigval).toString(),false); }
private static byte[] getUnsignedNegativeBytes(RubyInteger arg) { // calculation for negatives when %u specified // for values >= Integer.MIN_VALUE * 2, MRI uses (the equivalent of) // long neg_u = (((long)Integer.MAX_VALUE + 1) << 1) + val // for smaller values, BigInteger math is required to conform to MRI's // result. long longval; BigInteger bigval; if (arg instanceof RubyFixnum) { // relatively cheap test for 32-bit values longval = ((RubyFixnum)arg).getLongValue(); if (longval >= Long.MIN_VALUE << 1) { return ConvertBytes.longToCharBytes(((Long.MAX_VALUE + 1L) << 1) + longval); } // no such luck... bigval = BigInteger.valueOf(longval); } else { bigval = ((RubyBignum)arg).getValue(); } // ok, now it gets expensive... int shift = 0; // go through negated powers of 32 until we find one small enough for (BigInteger minus = BIG_MINUS_64 ; bigval.compareTo(minus) < 0 ; minus = minus.shiftLeft(32), shift++) {} // add to the corresponding positive power of 32 for the result. // meaningful? no. conformant? yes. I just write the code... BigInteger nPower32 = shift > 0 ? BIG_64.shiftLeft(32 * shift) : BIG_64; return stringToBytes(nPower32.add(bigval).toString(),false); }
private static byte[] getFixnumBytes(RubyFixnum arg, int base, boolean sign, boolean upper) { long val = arg.getLongValue(); // limit the length of negatives if possible (also faster) if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { if (sign) { return ConvertBytes.intToByteArray((int)val,base,upper); } else { switch(base) { case 2: return ConvertBytes.intToBinaryBytes((int)val); case 8: return ConvertBytes.intToOctalBytes((int)val); case 10: default: return ConvertBytes.intToCharBytes((int)val); case 16: return ConvertBytes.intToHexBytes((int)val,upper); } } } else { if (sign) { return ConvertBytes.longToByteArray(val,base,upper); } else { switch(base) { case 2: return ConvertBytes.longToBinaryBytes(val); case 8: return ConvertBytes.longToOctalBytes(val); case 10: default: return ConvertBytes.longToCharBytes(val); case 16: return ConvertBytes.longToHexBytes(val,upper); } } } }
private static byte[] getFixnumBytes(RubyFixnum arg, int base, boolean sign, boolean upper) { long val = arg.getLongValue(); // limit the length of negatives if possible (also faster) if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { if (sign) { return ConvertBytes.intToByteArray((int)val,base,upper); } else { switch(base) { case 2: return ConvertBytes.intToBinaryBytes((int)val); case 8: return ConvertBytes.intToOctalBytes((int)val); case 10: default: return ConvertBytes.intToCharBytes((int)val); case 16: return ConvertBytes.intToHexBytes((int)val,upper); } } } else { if (sign) { return ConvertBytes.longToByteArray(val,base,upper); } else { switch(base) { case 2: return ConvertBytes.longToBinaryBytes(val); case 8: return ConvertBytes.longToOctalBytes(val); case 10: default: return ConvertBytes.longToCharBytes(val); case 16: return ConvertBytes.longToHexBytes(val,upper); } } } }
private static byte[] getFixnumBytes(RubyFixnum arg, int base, boolean sign, boolean upper) { long val = arg.getLongValue(); // limit the length of negatives if possible (also faster) if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { if (sign) { return ConvertBytes.intToByteArray((int)val,base,upper); } else { switch(base) { case 2: return ConvertBytes.intToBinaryBytes((int)val); case 8: return ConvertBytes.intToOctalBytes((int)val); case 10: default: return ConvertBytes.intToCharBytes((int)val); case 16: return ConvertBytes.intToHexBytes((int)val,upper); } } } else { if (sign) { return ConvertBytes.longToByteArray(val,base,upper); } else { switch(base) { case 2: return ConvertBytes.longToBinaryBytes(val); case 8: return ConvertBytes.longToOctalBytes(val); case 10: default: return ConvertBytes.longToCharBytes(val); case 16: return ConvertBytes.longToHexBytes(val,upper); } } } }
private static byte[] getFixnumBytes(RubyFixnum arg, int base, boolean sign, boolean upper) { long val = arg.getLongValue(); // limit the length of negatives if possible (also faster) if (val >= Integer.MIN_VALUE && val <= Integer.MAX_VALUE) { if (sign) { return ConvertBytes.intToByteArray((int)val,base,upper); } else { switch(base) { case 2: return ConvertBytes.intToBinaryBytes((int)val); case 8: return ConvertBytes.intToOctalBytes((int)val); case 10: default: return ConvertBytes.intToCharBytes((int)val); case 16: return ConvertBytes.intToHexBytes((int)val,upper); } } } else { if (sign) { return ConvertBytes.longToByteArray(val,base,upper); } else { switch(base) { case 2: return ConvertBytes.longToBinaryBytes(val); case 8: return ConvertBytes.longToOctalBytes(val); case 10: default: return ConvertBytes.longToCharBytes(val); case 16: return ConvertBytes.longToHexBytes(val,upper); } } } }