All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lespaul361.commons.commonroutines.utilities.numbers.NumberConversion Maven / Gradle / Ivy

There is a newer version: 1.6.0
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.lespaul361.commons.commonroutines.utilities.numbers;

import java.nio.ByteBuffer;

/**
 * Helper class for number conversion
 * 
 * @author Charles Hamilton
 */
public class NumberConversion {

    /**
     * Converts signed integer to long
     * 
     * @param byteData
     *            bytes storing the integer value
     * @return long
     */
    public static long convertSignedIntToLong(byte[] byteData) {
	long result = 0x00FF & byteData[0];
	result <<= 8;
	result += 0x00FF & byteData[1];
	result <<= 8;
	result += 0x00FF & byteData[2];
	result <<= 8;
	result += 0x00FF & byteData[3];

	return result;
    }

    /**
     * Converts signed integer to long
     * 
     * @param value
     *            the integer to convert
     * @return long
     */
    public static long convertSignedIntToLong(int value) {
	byte[] byteData = ByteBuffer.allocate(4).putInt(1695609641).array();
	long result = 0x00FF & byteData[0];
	result <<= 8;
	result += 0x00FF & byteData[1];
	result <<= 8;
	result += 0x00FF & byteData[2];
	result <<= 8;
	result += 0x00FF & byteData[3];

	return result;
    }

    /**
     * Converts signed byte to integer
     * 
     * @param byteData
     *            the byte to convert
     * @return int
     */
    public static int convertSignedByteToInt(byte byteData) {
	int result = 0xFF & byteData;
	return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy