实现PHP实现INT型,SHORT型,STRING转换成BYTE数组的转化:

class Bytes {
  public static function integerToBytes($val) {
    $val = (int)$val;
    $byte = array();
    //低位在前,即小端法表示
    $byte[0] = ($val & 0xFF);//掩码运算
    $byte[1] = ($val >> 8 & 0xFF);
    $byte[2] = ($val >> 16 & 0xFF);
    $byte[3] = ($val >> 24 & 0xff);
    return $byte;
  }
 
  public static function bytesToInteger(array $bytes, $pos) {
    $val = 0;
    $val = $bytes[$pos + 3] & 0xff;
    $val <<= 8;
    $val |= $bytes[$pos + 2] & 0xff;
    $val <<= 8;
    $val |= $bytes[$pos + 1] & 0xff;
    $val <<= 8;
    $val |= $bytes[$pos + 0] & 0xff;
    return intval($val);
  }
 
  public static function shortToBytes($val) {
    $val = intval($val);
    $byt = array();
    $byt[0] = ($val & 0xff);
    $byt[1] = ($val >> 8 & 0xff);
    return $byt;
  }
 
  public static function bytesToShort($bytes) {
    $val = 0;
    $val = $bytes[1] & 0xff;
    $val <<= 8;
    $val |= $bytes[0] & 0xff;
    return $val;
  }
 
  public static function String2AsciiArray($str) {
    $str = (string) $str;
    $strLength = strlen($str);
    $rtn = array();
    for($i = 0; $i < $strLength; $i++) {
      $rtn[]= ord($str[$i]);
    }
    return $rtn;
  }
}

PHP实现INT型,SHORT型,STRING转换成BYTE数组的更多相关文章

  1. 将文件转换成byte[]数组

    代码 /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl&quo ...

  2. Map 转换成byte[] 数组

    把Map转换成byte数组,使用 ByteArrayOutputStream和ObjectOutputStream Map<String,String> map = new HashMap ...

  3. 将文件File转换成byte数组

    代码如下: /** * 将文件转换成byte数组 * @param filePath * @return */ public static byte[] File2byte(File tradeFil ...

  4. JAVA将文件转换成byte数组(byte[])

    /** * 将文件转换成byte数组 * @param filePath 文件File类 通过new File(文件路径) * @return byte数组 */ public static byte ...

  5. 怎样将short[]数组转换成byte[]数组

    byte[] byteArray = Array.ConvertAll<short, byte>(shortArray, Convert.ToByte);

  6. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  7. 在C#中将String转换成Enum:

    一:  在C#中将String转换成Enum: object Enum.Parse(System.Type enumType, string value, bool ignoreCase); 所以,我 ...

  8. C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)

    在c#中,string类型转换成DateTime类型是经常用到的,作为基本的知识,这里在此做个小结.一般来说可以使用多种方法进行转换,最常用的就是使用Convert.ToDateTime(string ...

  9. string转换成color转

    string转换成color             string col = "#FF8400";            this.BackColor = System.Draw ...

随机推荐

  1. 【贪心】「poj1328」Radar Installation

    建模:二维转一维:贪心 Description Assume the coasting is an infinite straight line. Land is in one side of coa ...

  2. Ecshop的积分商城-对不起,该商品库存不足,现在不能兑换

    1. 打开Ecshop积分商城文件 "根目录/exchange.php" 发现248行与289行都有库存不足时报错的提示代码: 248行:     /* 查询:检查兑换商品是否有库 ...

  3. Python Third Day-函数

    ''' 为什么要有函数?没有函数带来的困扰? 组织结构不清晰,可读性差 代码冗余 可扩展性差 什么是函数 具备某一个功能的工具--->函数 事先准备工具->函数的定义 拿来就用.重复使用- ...

  4. fsm三种建模思路比较

    ==================================================================================================== ...

  5. 创建虚拟机、安装centos系统,xshell连接虚拟机

    创建虚拟机 文件--->新建虚拟机--->自定义最高级 选择虚拟机兼容性: workstation12.0:限制少,而且兼容的多 稍后安装操作系统 选择Linux系统 可选择centos7 ...

  6. linux-命令学习-1

    1. cat命令 http://blog.csdn.net/jackalfly/article/details/7556848 cat主要有三大功能:1.一次显示整个文件.$ cat   filena ...

  7. Codeforces Round #877 (Div. 2) E. Danil and a Part-time Job

    E. Danil and a Part-time Job 题目链接:http://codeforces.com/contest/877/problem/E time limit per test2 s ...

  8. spring配置datasource三种方式 数据库连接池

    尊重原创(原文链接):http://blog.csdn.net/kunkun378263/article/details/8506355 1.使用org.springframework.jdbc.da ...

  9. mysql无法启动,报错 Can't start server: can't create PID file: No space left on device

    然后看mysql日志文件 出现Can't start server: can't create PID file: No space left on device 这个错误. 提示磁盘空间不足 后用d ...

  10. tinyMCE获取鼠标选中的值

    今天遇到一个需求就是要在WordPress的文章编辑页面增加选中文字时要将选中值传递到弹出窗口中 解决方法: 去网上找到了下面这段代码,可以获取到选中文本 { type: 'textbox', nam ...