zoukankan      html  css  js  c++  java
  • Android常用数据类型转换

    String转intfloat、double、byte[]、bitmap

    Int i = Integer.parseInt(str);
     
    Float f = Float.parseFloat(str);
     
    Double d = Double.parseDouble(str);   
    
    //将16进制字符串转byte数组
    
    public static byte[] hexStringToByte(String str) {
            if(str == null || str.trim().equals("")) {
                 return new byte[0];
            }
           byte[] bytes = new byte[str.length() / 2];
            for(int i = 0; i < str.length() / 2; i++) {
                String subStr = str.substring(i * 2, i * 2 + 2);
                bytes[i] = (byte) Integer.parseInt(subStr, 16);
            }
            return bytes;
    }
    
    String.format("%04x", i);//将10进制整形转16进制字符串,%04x2字节表示不足位补0
    
    //将String字符串转回Bitmap
    
    public Bitmap stringToBitmap(String string) {
          Bitmap bitmap = null;
          try {
               byte[] bitmapArray;
               bitmapArray = Base64.decode(string, Base64.DEFAULT);
               bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,bitmapArray.length);
          } catch (Exception e) {
              e.printStackTrace();
          }
          return bitmap;
    }

     Int转string、byte[]

    String str = String.valueOf(i);//效率最高
    
    //将Int转byte[]数组
    
    public static byte[] intToBytes2(int n){
          byte[] b = new byte[4];
          for(int i = 0;i < 4;i++){
              b[i] = (byte)(n >> (24 - i * 8)); 
          }
          return b;
    }

     Byte[]转string、int、bitmap

    //byte数组转16进制字符串
    
    private String bytes2HexString(byte[] b, int length) {
          StringBuilder r = new StringBuilder();
          for (int i = 0; i < length; i++) {
               String hex = Integer.toHexString(b[i] & 0xFF);
               if (hex.length() == 1) {
                  hex = "0" + hex;
               }
               r.append(hex.toUpperCase());
         }
          return r.toString();
    }
    
    //byte数组转16进制字符串
    
    public static int byteToInt(byte[] b) {
          int mask=0xff;
          int temp=0;
          int n=0;
          for(int i=0;i<b.length;i++){
              n<<=8;
              temp=b[i]&mask;
              n|=temp;
          }
          return n;
    }
    
    //byte数组转bitmap
    
    byte[] b = getIntent().getByteArrayExtra("bitmap");  
    Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length); 
    
    
    //将byte数组以16进制的形式打印到控制台
    
    public static void printHexString( byte[] b) {
        StringBuilder str= new StringBuilder();
        for (byte aB : b) {
            String hex = Integer.toHexString(aB & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            str.append(hex.toUpperCase()).append(" ");
        }
        Log.i("cmd", str.toString());
    }

    Bitmap转string、byte[]

    //将Bitmap转base64字符串
    
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG,90,outputStream );//压缩90%
    byte[] imagebyte = outputStream.toByteArray();
    String imageStr = Base64.encode(imagebyte);
    
    //将Bitmap转byte[]
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] datas = baos.toByteArray();

     View转Bitmap

    public static Bitmap view2Bitmap(View view) {
          if (view == null) return null;
          Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(ret);
          Drawable bgDrawable = view.getBackground();
          if (bgDrawable != null) {
              bgDrawable.draw(canvas);
          } else {
              canvas.drawColor(Color.WHITE);
          }
          view.draw(canvas);
          return ret;
    }

     Gson高精度String、Float[]互转(测试可保留6位数以上)

    //Float[]转String
    
    float feature[] = new float[256];
    Gson gson = new Gson();
    String str = gson.toJson(feature.clone());
    
    //String高精度还原Float[]
    
    Gson gson = new Gson();
    float[] f = gson.fromJson(str, float[].class);

     CRC16检验

    private int CRC16_Check(byte Pushdata[]){
            int Reg_CRC=0xffff;
            int temp;
            int i,j;
            //帧头校验字去掉
            for( i = 2; i<Pushdata.length-2; i ++)
            {
                temp = Pushdata[i];
                if(temp < 0) temp += 256;
                temp &= 0xff;
                Reg_CRC^= temp;
    
                for (j = 0; j<8; j++)
                {
                    if ((Reg_CRC & 0x0001) == 0x0001)
                        Reg_CRC=(Reg_CRC>>1)^0xA001;
                    else
                        Reg_CRC >>=1;
                }
            }
            return (Reg_CRC&0xffff);
        }

     

     

  • 相关阅读:
    0Day – 2011.1.20[From B4A]
    0Day – 2011.1.16[From B4A]
    ubuntu 桌面下方的面板(任务栏)恢复方法
    Delphi WebBrowser用法几则浅谈
    0Day – 2011.1.3[From B4A]
    0Day – 2011.1.8[From B4A]
    0Day – 2011.1.6[From B4A]
    0Day – 2011.1.10[From B4A]
    0Day – 2011.01.21[From B4A]
    0Day – 2011.1.7[From B4A]
  • 原文地址:https://www.cnblogs.com/94xiyang/p/9359577.html
Copyright © 2011-2022 走看看