/* * Copyright (C) 2022 github.com/REAndroid * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.reandroid.arsc.decoder; public class ComplexUtil { public static String decodeComplex(boolean fraction, int complex_value){ int radixFlag = (complex_value >> COMPLEX_RADIX_SHIFT) & COMPLEX_RADIX_MASK; Radix radix = Radix.forFlag(radixFlag); long mantissa = (complex_value >> COMPLEX_MANTISSA_SHIFT) & COMPLEX_MANTISSA_MASK; mantissa = mantissa << radix.getShift(); float value = mantissa * MANTISSA_MULTIPLIER; int unit_type = (complex_value >> COMPLEX_UNIT_SHIFT) & COMPLEX_UNIT_MASK; Unit unit = Unit.fromFlag(fraction, unit_type); return radix.formatFloat(fraction, value) + unit.getSymbol(); } public static int encodeComplex(float value, String unit){ return encodeComplex(value, Unit.fromSymbol(unit)); } public static int encodeComplex(float value, Unit unit){ boolean neg = value < 0; if (neg) { value = -value; } long bits = (long)(value*(1<<23) + 0.5f); Radix radix = Radix.getRadix(bits); int mantissa = (int)((bits>>radix.getShift()) & COMPLEX_MANTISSA_MASK); if (neg) { mantissa = (-mantissa) & COMPLEX_MANTISSA_MASK; } int result = (radix.getFlag()<