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. C#学习笔记-抽象工厂模式

    题目1:数据访问,通过数据库对用户表单的进行访问,数据库包含SQL Server,对用户表单进行“新增用户”和“查询用户”信息等操作. 分析: 首先,确认用户表单,里面包含两个ID和Name两个字段, ...

  2. DNS Prefetch初认识

    今天在看一个后台框架时,发现这样的代码: <link rel="dns-prefetch" href="//0.s3.envato.com">< ...

  3. JAXB应用实例

    过往的项目中数据存储都离不开数据库,不过最近做的一个项目的某些数据(比如人员信息.菜单.权限等等)却完全没有涉及任何数据库操作,直接XML搞定.这里无意比较优劣,因为数据库存储和XML存储本就有不同的 ...

  4. Machine Learning - week 4 - Non-linear Hypotheses

    为什么计算机图像识别很难呢?因为我们看到的是汽车,而计算机看到的是表示颜色的 RGB 数值.计算机需要根据这些数值来判断. 如果图片是 50 * 50 像素,那么一共有 2500 个像素点.如果是 Q ...

  5. CTF---Web入门第十二题 程序逻辑问题

    程序逻辑问题分值:20 来源: 实验吧 难度:中 参与人数:6909人 Get Flag:1993人 答题人数:2070人 解题通过率:96% 绕过 解题链接: http://ctf5.shiyanb ...

  6. SRM 20

    本来T1想出给一堆数求异或最大值的,然后觉得太模板不好……就改成了现在这样(好像跟T2难度差不多了?不管辣 魔法弹 先把最大线性无关组求出来,就不会有重复的问题了.接下来单独考虑每个位,如果某个上所有 ...

  7. UESTC30-最短路-Floyd最短路、spfa+链式前向星建图

    最短路 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 在每年的校赛里,所有进入决赛的同 ...

  8. Ajax 案例之三级联动

    每次在博客园网站写博客,格式真的好难搞,还望好心人告知更好的编辑工具.接下来进入正题:三级联动(其效果演示可看我的博文Ajax 学习总结 末尾). 数据表设计(Oracle) 新建数据表 Employ ...

  9. Git服务搭建及github使用教程

    .pos { position: fixed; top: 35%; left: 90% } .pos a { border: 2px solid white; background: #99CCFF; ...

  10. jquery 和 mui 上拉加载

    jquery: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <m ...