package com.yqw.java.util;

/**
 * 数字转换工具
 */
public class MathUtils {

    /**
     * short转byte
     */
    public static byte[] toBytes(short s)
    {
        return new byte[] { (byte)(s & 0x00FF), (byte)((s & 0xFF00) >> 8) };
    }

    /**
     * unsigned short转byte
     */
    public static byte[] unsignedShortToBytes(int s)
    {
        return new byte[] { (byte)(s & 0x000000FF),
                           (byte)((s & 0x0000FF00) >> 8) };
    }

    /**
     * int转byte
     */
    public static byte[] toBytes(int s)
    {
        return new byte[] { (byte) (s & 0x000000FF),
                            (byte)((s & 0x0000FF00) >> 8),
                            (byte)((s & 0x00FF0000) >> 16),
                            (byte)((s & 0xFF000000) >> 24) };
    }

    /**
     * byte转int
     */
    public static  int toInt(byte[] b)
    {
        return b[0] & 0xff  | (b[1] & 0xff) << 8| (b[2] & 0xff) << 16
                | (b[3] & 0xff<< 24);
    }
    
    /**
     * byte转long
     */
    public static  long toUnsignedInt(byte[] b)
    {
        return  b[0]& 0xff  | (b[1] & 0xff) << 8| (b[2] & 0xff) << 16
        | (b[3] << 24);
    }

    /**
     * byte转short
     */
    public static  short toShort(byte[] b)
    {
//        return (short) (b[0] << 24 | (b[1] & 0xff) << 16) ;
        return (short) (((b[1] << 8) | b[0] & 0xff));
    }
    
    /**
     * byte转unsigned short
     */
    public static  int toUnsignedShort(byte[] b)
    {
        return (b[0] << 24 | (b[1] & 0xff) << 16) ;
    }
    
    /**
     * Assume the long is used as unsigned int
     * @param s
     * @return
     */
    public static byte[] unsignedIntToBytes(long s)
    {
        return new byte[] { (byte) (s & 0x00000000000000FF),
                            (byte)((s & 0x000000000000FF00) >> 8),
                            (byte)((s & 0x0000000000FF0000) >> 16),
                            (byte)((s & 0x00000000FF000000) >> 24) };             
    }
    
    
    /**
     * float转换byte
     *
     * @param x
     * @param index
     */
    public static byte[] putFloat(float x) {
        byte[] b = new byte[4];
        int l = Float.floatToIntBits(x);
        for (int i = 0; i < 4; i++) {
            b[i] = new Integer(l).byteValue();
            l = l >> 8;
        }
        return b;
    }

    /**
     * 通过byte数组取得float
     *
     * @param b
     * @return
     */
    public static float getFloat(byte[] b) {
        int l;
        l = b[0];
        l &= 0xff;
        l |= ((long) b[1] << 8);
        l &= 0xffff;
        l |= ((long) b[2] << 16);
        l &= 0xffffff;
        l |= ((long) b[3] << 24);
        return Float.intBitsToFloat(l);
    }

}

MathUtils的更多相关文章

  1. 科学计算法帮助类MathUtils

    import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; /** * 科学计算 ...

  2. MathUtils BigDecimal 数字工具类

    package com.hxqc.basic.dependency.util; import org.apache.commons.lang.StringUtils; import java.math ...

  3. Blender 脚本之 Operator 初探

    addon(插件)用来扩展 Blender 的功能,跟其他软件里的 plugin(插件)一样,去掉不会影响软件的运行.插件可以加到 Blender 的用户偏好设置目录里,或者就在你所编辑的.blend ...

  4. 你应该知道的那些Android小经验

    原文出处:http://jayfeng.com/ 做Android久了,就会踩很多坑,被坑的多了就有经验了,闲暇之余整理了部分,现挑选一些重要或者偏门的“小”经验做个记录. 查看SQLite日志 ad ...

  5. android opengl

    引用:http://weimingtom.iteye.com/blog/1616972 二维坐标系变换为原点在左上角(测试用) * GLES * JOGL * LWJGL * libgdx(使用g2d ...

  6. (Python)导出指定文件夹中as文件的完全限定类名

    AS3程序在编译的过程中,有一个特点是这样的,不管是项目中的类,还是标准库或者第三方库的类,编译的时候只会把用到的那些类文件编译进去,也就是说,某一些类,只要没有被主程序引用到,那这个文件是不会被编译 ...

  7. 算法(二)之遗传算法(SGA)

    算法(二)之遗传算法(SGA) 遗传算法(Genetic Algorithm)又叫基因进化算法或进化算法,是模拟达尔文的遗传选择和自然淘汰的生物进化过程的计算模型,属于启发式搜索算法一种. 下面通过下 ...

  8. Guava 12-数学运算

    范例 int logFloor = LongMath.log2(n, FLOOR); int mustNotOverflow = IntMath.checkedMultiply(x, y); long ...

  9. Scut游戏服务器免费开源框架--快速开发(2)

    Scut快速开发(2) Python脚本开发 1   开发环境 Scut Lib版本:5.2.3.2 需要安装的软件 a)        IIS和消息队列(MSMQ) 进入控制面板,程序和功能 b)  ...

随机推荐

  1. Effective Java 第三版—— 20. 接口优于抽象类

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  2. Python_day1

    一.HelloWorld >>>print("Hello World!") >>>Hello World! 二.变量    1.什么是变量 : ...

  3. 在visual studio的工程项目应用中打开console控制窗口

    在visual studio的工程项目应用中打开console控制窗口,这个可以方便我们在console中输出参数的值检查错误. 只需要在需要打开console的地方加入下面的代码即可. AllocC ...

  4. 385cc412a70eb9c6578a82ac58fce14c 教大家破解md5验证值

    Md5密文破解(解密)可以说是网络攻击中的一个必不可少的环节,是工具中的一个重要"辅助工具".md5解密主要用于网络攻击,在对网站等进行入侵过程,有可能获得管理员或者其他用户的账号 ...

  5. Java string和各种格式互转 string转int int转string

    Java string和各种格式互转 string转int int转string 简单收集记录下 其他类型转String String s = String.valueOf( value); // 其 ...

  6. 栈的存储结构的实现(C/C++实现)

    存档 #include "iostream.h" #include <stdlib.h> #define max 20 typedef char elemtype; # ...

  7. 洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He…【字符串+模拟】

    P1200 [USACO1.1]你的飞碟在这儿Your Ride Is He… 题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都 ...

  8. HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)

    话不多说,日常一水题,水水更健康!┗|`O′|┛ 嗷~~ a/b + c/d Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768 ...

  9. Spring的IOC分析(一)

    我们学习Spring之前需要对23种java的设计模式的9种有一定的理解,设计模式为了解耦,Spring也是在解耦的方向上设计的,所以设计模式要理解一下,它当中用到了很多. 单例模式(写法很多钟,7种 ...

  10. angular 表达式与指令

    angular表达式的一些特点 属性表达式: 属性表达式是对应于当前作用域,Javascript对应的是全局window对象. AngularJS要使用window作用域的话得用$window来指向全 ...