Flutter list int 類型轉文字

我們在使用Socket進行請求的時候  接收到的返回值一般也是list<int>類型  有時候需要轉換成字符串   所以自己寫了一個工具類

下面我貼出方法   供大家參考

1將List<int>轉換爲int值

    //將數組轉換成int值  一般接收的時候需要
    // ignore: missing_return
    int  listToValue(List<int> data){
    //data  [a0,a1]
        switch(data.length){
          case 2:
            return (data[1]<<8)+data[0];

            break;
          case 4:
            return data[0]+(data[1]<<8)+(data[2]<<16)+(data[3]<<24);

            break;
          case 8:
            return data[0]+(data[1]<<8)+(data[2]<<16)+(data[3]<<24)+
                (data[4]<<32)+(data[5]<<40)+(data[6]<<48)+(data[7]<<56);

            break;

          case 16:
            return data[0]+(data[1]<<8)+(data[2]<<16)+(data[3]<<24)+
                    (data[4]<<32)+(data[5]<<40)+(data[6]<<48)+(data[7]<<56)+
                    (data[8]<<64)+(data[9]<<72)+(data[10]<<80)+(data[11]<<88)+
                    (data[12]<<96)+(data[13]<<104)+(data[14]<<112)+(data[15]<<116);

            break;

        }
    }

2.因爲每個公司的業務不同  所以  大家按照自己公司的業務來截取對應位置的數據 


    //將unicode或ascii編碼類型的list(編碼格式 長度 字符串內容)轉換爲String字符串
    String unicodeOrAsciiList2String(List<int> data){
           //獲取字符串長度對應的數據
         List<int> lengthList= data.sublist(1,3);
            //獲取字符串長度的int值
         int length=listToValue(lengthList);
         if(length==0){     //長度爲0
           return '';
         }else{             //長度不爲0  
          List<int> strList= data.sublist(3,data.length);
          String result='';
           if(data[0]==1){       //ASCII編碼
                strList.forEach((value){
                    result+=String.fromCharCode(value);
                });
                return result;
           }else if(data[0]==2){ //UNICODE編碼
              for(int i=0;i<strList.length~/2;i++){
                 List<int> current=  strList.sublist(i*2,(i+1)*2);
                result+=String.fromCharCode(listToValue(current));
              }
              return result;
           }
         }
    }


   //字符串轉ascii編碼數組
    List<int> str2AsciiList(String text){
      List<int> temp=text.codeUnits;
      List<int> result=[];
      List<int> lengthList= HexadecimalConversionUtils().valueToList(temp.length, 2);
      result..add(1)..addAll(lengthList)..addAll(temp);  //編碼格式  長度  內容
      return result;
    }




    //將字符串轉換爲unicode格式的   編碼格式  長度  字符串內容 形式的list集合
    List<int> str2unicodeList(String text){
      List<int> temp=text.codeUnits;
      List<int> textList=List();
      List<int> result=[2];  //unicode編碼
        for(int i=0;i<temp.length;i++){
           List<int> charList= valueToList(temp[i],2);
           charList.reversed;
           textList.addAll(charList);
        }
      List<int>  textLengthList=valueToList(textList.length, 2);
        result..addAll(textLengthList)..addAll(textList);
        return result;
    }

HexadecimalConversionUtils:

 //將一個Int值轉化爲數組  一般發送時候需要
      // ignore: missing_return
      List<int> valueToList(int value,int length){
       switch(length){
         case 2:
            int a0=value&0xff;
            int a1=(value>>8)&0xff;
           return [a0,a1];

           break;
         case 4:
           int a0=value&0xff;
           int a1=(value>>8)&0xff;
           int a2=(value>>16)&0xff;
           int a3=(value>>24)&0xff;
           return [a0,a1,a2,a3];

           break;
         case 8:
           int a0=value&0xff;
           int a1=(value>>8)&0xff;
           int a2=(value>>16)&0xff;
           int a3=(value>>24)&0xff;
           int a4=(value>>32)&0xff;
           int a5=(value>>40)&0xff;
           int a6=(value>>48)&0xff;
           int a7=(value>>56)&0xff;
           return [a0,a1,a2,a3,a4,a5,a6,a7];

           break;

       }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章