美文网首页Flutter学习
Flutter (dart) int 跟 byte[]之间的互

Flutter (dart) int 跟 byte[]之间的互

作者: plmman | 来源:发表于2020-09-07 14:08 被阅读0次

    byte[]转int

    
    class Bytes2int{
    
    static convert(List list){
    
    var length = list.length;
    
    switch(length){
    
    case 1:
    
    return list[0];
    
    case 2:
    
    return toInt16(list);
    
    case 4:
    
    return toInt32(list);
    
    default:
    
    print('传入数据不对');
    
    break;
    
    }
    
    }
    
    //byte[] to int
    
      static int toInt16( List list, {int index=0})
    
    {
    
    Uint8List byteArray =Uint8List.fromList(list);
    
    ByteBuffer buffer = byteArray.buffer;
    
    ByteData data =new ByteData.view(buffer);
    
    int short = data.getInt16(index, Endian.big);
    
    return short;
    
    }
    
    static int toInt32( List list, {int index=0})
    
    {
    
    Uint8List byteArray =Uint8List.fromList(list);
    
    ByteBuffer buffer = byteArray.buffer;
    
    ByteData data =new ByteData.view(buffer);
    
    int short = data.getInt32(index, Endian.big);
    
    return short;
    
    }
    
    static int toInt64( List list, {int index=0})
    
    {
    
    Uint8List byteArray =Uint8List.fromList(list);
    
    ByteBuffer buffer = byteArray.buffer;
    
    ByteData data =new ByteData.view(buffer);
    
    int short = data.getInt64(index, Endian.big);
    
    return short;
    
    }
    
    }
    
    

    int 转 byte[]

    
    class int2Bytes{
    
    static convert(int source,{Type type=Type.WORD}){
    
    var s = source.toRadixString(16);
    
    var pre ='0';
    
    if(s.length%2==1) {
    
    s = pre+s;
    
    }
    
    List list = [];
    
    var uint8list = Hex.createUint8ListFromHexString(s);
    
    switch(type){
    
    case Type.BYTE:
    
    break;
    
    case Type.WORD:
    
    if(uint8list.length==1){
    
    list.add(0);
    
    }
    
    break;
    
    case Type.DWORD:
    
    for(var i =0;i<4-uint8list.length;i++) {
    
    list.add(0);
    
    }
    
    break;
    
    }
    
    list.addAll(uint8list);
    
    return list;
    
    }
    
    }
    
    enum Type{
    
    BYTE,//1
    
      WORD,//2
    
      DWORD,//4
    
      STRING
    
    }
    
    

    相关文章

      网友评论

        本文标题:Flutter (dart) int 跟 byte[]之间的互

        本文链接:https://www.haomeiwen.com/subject/baokektx.html