实现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. 将Xcode的本地代码push到github仓库上

    1.首先,你得有一个github账号,如果没有的话就去注册一个,通过下面图片的方式创建一个github仓库. 2.创建仓库后填写相关的信息,比如说仓库名等. 3.在xcode上进行设置,添加远程git ...

  2. linux 5.7.20和5.6.38版本 数据库忘记root密码怎么找回?

    1.    5.6.38版本的数据库密码丢失找回方法: 第一步.关数据库 第二步:mysqld_safe --skip-grant-tables --skip-networking & 第三步 ...

  3. Zookeeper 集群 BindException: Cannot assign requested address 解决方案

    前言 经历: 最近在搭建zookeeper集群,基础是3台机器(尝试过ubuntu 17 和 Centos 7). 一开始选择的是3台腾讯云服务器,每台机器在java环境配置正确的情况下,单机的情况都 ...

  4. Python_sort函数结合functools.cmp_to_key(func)分析

    举例如下: from functools import cmp_to_key persons = [ { 'name':'zhangsan', 'age':20, 'grade':98 }, { 'n ...

  5. checkStyle使用手册

    1. Annotations(注解:5个) Annotation Use Style(注解使用风格) 这项检查可以控制要使用的注解的样式. Missing Deprecated(缺少deprecad) ...

  6. linux 使用mail 发送邮件

    配置: /etc/mail.rc 追加配置参数 set from=lynctest@iclinux.com smtp="mail.iclinux.com"smtp-auth-use ...

  7. PYDay2-linux基础\常用命令

    一.linux 理念 一切皆文件 二.常用命令(150) 2.1.rsync rsync是类unix系统下的数据镜像备份工具, 它的特性如下: 可以镜像保存整个目录树和文件系统. 可以很容易做到保持原 ...

  8. c++ string char* 获取输入值的区别

    #include <iostream> #include <string> using namespace std; void reverseStr(string &s ...

  9. 贴一下我写过的c++程序代码

    5258 #include <iostream>#include <iomanip>#include <cmath>using namespace std;clas ...

  10. Linux 查看端口占用并杀掉进程

    1. 查看端口号占用情况: netstat -apn|grep 11305 tcp        0      0 10.65.42.27:80              172.22.142.20: ...