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. 浅谈Android的广告欢迎界面(倒计时)

    前些时候就是别人问我他的android APP怎么做一个广告的欢迎界面,就是过几秒后自动跳转到主界面的实现. 也就是下面这种类似的效果.要插什么广告的话你就换张图吧. 那么我就思考了下,就用了andr ...

  2. Linux内核打印时间戳

    较为简单可行的方式是通过PrintkTime功能为启动过程的所有内核信息增加时间戳,便于汇总分析.PrintkTime最早为CELF所提供的一个内核补丁,在后来的Kernel 2.6.11版本中正式纳 ...

  3. 使用三种方法求解前N个正整数的排列

    本篇博文给大家介绍前N个正整数的排列求解的三种方式.第一种是暴力求解法:第二种则另外声明了一个长度为N的数组,并且将已经排列过的数字保存其中:第三种方式则采用了另外一种思路,即首先获取N个整数的升序排 ...

  4. CodeForces801-A.Vicious Keyboard-暴力

    A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. B. Secret Combination

    B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. JAVA爬虫实践(实践四:webMagic和phantomjs和淘宝爬虫)

    webMagic虽然方便,但是也有它不适用的地方,比如定向的某个单页面爬虫,或者存在大量ajax请求,页面的跳转请求全都混淆在js里. 这时可以用webMagic结合phantomjs来真实模拟页面请 ...

  7. Socket send函数和recv函数详解

    1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP ...

  8. APP测试时常用adb命令

    ADB全称Android Debug Bridge, 是android sdk里的一个工具, 用这个工具可以直接操作管理android模拟器或者真实的andriod设备(手机),故在其实工作可以给我们 ...

  9. vueThink权限配置

    vueThink中的 admin 默认是展示所有权限,其他的权限组用户就要自己去特定进行配置 http://vuedemo.cn:8181 这里我是默认本地配置了apache到  php\public ...

  10. navicat将多个表导出为一个sql文件

    1.shift选中多个表 2右键选择--转储sql文件---结构和数据