zoukankan      html  css  js  c++  java
  • ByteArray的操作总结(复制、打印、位运算)

    1. 字节数组的复制

    Method A:Array.Clone()

    Clone返回的是Object对象,需要强类型转换;Clone会创建一个新的对象,并将value赋给dec

    byte[] src = new byte[20];
    byte[] dst= new byte[20];
    dst = (byte[])src.Clone();

    Method B: Array.Coby

    有多个重载版本

    byte[] src = new byte[20];    //原数组
    byte[] dst = new byte[20];    //目标数组
    int srcOffset = 0;    //原数组偏移量
    int dstOffset = 0;    //目标数组偏移量
    Array.Copy(src, srcOffset, dec, dstOffset, dst.Length);

    Method C: Buffer.BlockCopy

    该方法最为常用

    byte[] src = new byte[20];    //原缓冲区
    byte[] dst = new byte[20];    //目标缓冲区
    int srcOffset = 0;    //src字节偏移量,从0开始
    int dstOffset = 0;    //dst字节偏移量,从0开始
    Buffer.BlockCopy(src, srcOffset, dst, dstOffset, dst.Length);

    2. 字节数组的转换

    Method A: Print ByteArray

    byte[] array = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04 };
    string str = BitConverter.ToString(array);
    //str = "00-01-02-03-04"

    Method B: Encoding.Default.GetString(byteArray)

    将数组中的所有字节解析为应字符串

    byte[] array = new byte[] { 0xFE, 0xFE, 0xFE };
    string str = Encoding.ASCII.GetString(array);
    //str = "???"

    Method C: 源类型未知

    byte[] src = new byte[] { 0xFE, 0xFE, 0xFE };
    StreamReader sr = new StreamReader(new MemoryStream(src));
    string str = sr.ReadToEnd();
    //str = "???"

    3. 字符串转字节数组: Encoding.Default.GetBytes()

    string str = "???";
    byte[] bytes = Encoding.Default.GetBytes(str);
    //bytes = [0xFE, 0xFE, 0xFE, 0xFE]
  • 相关阅读:
    走进AngularJs(二) ng模板中常用指令的使用方式
    mysql知识汇总
    存储动态数据时,数据库的设计方法
    js判断密码强度
    svg―Raphael.js Library(一)
    常用js代码
    IE6实现图片或背景的圆角效果
    巧用css border
    IE6下的效果
    理解盒模型
  • 原文地址:https://www.cnblogs.com/mahuanpeng/p/6443676.html
Copyright © 2011-2022 走看看