String转int、float、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);
}

Android常用数据类型转换的更多相关文章

  1. Android 常用数据适配器SimpleAdapter

    在<Android 常用数据适配器ArrayAdapter>中介绍了ArrayAdapter数据适配器.但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用 ...

  2. Python基础学习笔记(九)常用数据类型转换函数

    参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...

  3. 泛型集合、datatable常用数据类型转换Json帮助类

    泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...

  4. Android 常用数据适配器ArrayAdapter

    接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...

  5. Python常用数据类型转换

    常用的数据类型转换 目标 了解类型转换的作用 掌握常用的类型转换 函数 说明 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ...

  6. python运算符和常用数据类型转换

    运算符 算术运算符 运算符 描述 实例 + 加 两个对象相加 a + b 输出结果 30 - 减 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 两个数相乘或是返回一个被重复若干 ...

  7. java常用数据类型转换

    在Java开发过程中经常会涉及到数据类型的转换问题,比如数字型转字符型,字符型转日期型,字符串转数组等等,以及其他类型的强制转换等.经常出现,所以有必要总结一下. 1.如何将字串 String 转换成 ...

  8. Android 常用数据操作封装类案例

    1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...

  9. 【Java】常用数据类型转换(BigDecimal、包装类、日期等)

    新工作转到大数据方向,每天都要面对数据类型互相转换的工作,再加上先前面试发现这部分的知识盲点, 决定复习之余自己再写一套便捷的方法,以后会比较方便.(虽然公司有现成封装的类,里头还有些遗漏的地方,暂时 ...

随机推荐

  1. Python函数学习——作用域与嵌套函数

    全局与局部变量 在函数中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量. 全局变量作用域是整个程序,局部变量作用域是定义该变量的函数. 当全局变量与局部变量同名时,在定义局部变量的函数内 ...

  2. cannot download, /home/azhukov/go is a GOROOT, not a GOPATH

    问题详情: go环境安装好后,运行go代码也没有问题 下载govendor包的时候提示: cannot download, /home/azhukov/go is a GOROOT, not a GO ...

  3. 14-03 java BigInteger类,BigDecimal类,Date类,DateFormat类,Calendar类

    BigInteger类 发 package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以让超过Integer范围内的数据进 ...

  4. Python:SQLMAP参数中文解释

    #HiRoot's BlogOptions(选项):--version 显示程序的版本号并退出-h, --help 显示此帮助消息并退出-v VERBOSE 详细级别:0-6(默认为1) Target ...

  5. 从零开始学 Web 之 DOM(六)为元素绑定与解绑事件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...

  6. 逆向知识之CS辅助/外挂专题.2.实现CS1.6无限夜视仪.无限闪光烟雾高爆弹.

    逆向知识之CS辅助/外挂专题.2.实现CS1.6无限夜视仪.无限闪光烟雾高爆弹. 关于人物子弹无限可以观看上一篇博客. 一丶无限夜视仪. 无限夜视仪找法. 1.CE附加游戏. 2.搜索0或者1. 3. ...

  7. 解决 "Script Error" 的另类思路

    本文由小芭乐发表 前端的同学如果用 window.onerror 事件做过监控,应该知道,跨域的脚本会给出 "Script Error." 提示,拿不到具体的错误信息和堆栈信息. ...

  8. 这次聊聊Promise对象

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由前端林子发表于云+社区专栏 Promise是CommonJS提出的一种规范,在ES6中已经原生支持Promise对象,非ES6环境可以 ...

  9. SpringBoot(3) 文件上传和访问

    springboot文件上传 MultipartFile file,源自SpringMVC MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutS ...

  10. [Luogu 3787] 冰精冻西瓜

    Description 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有根树,琪露诺想要把它们冷冻起来慢慢吃. 这些西瓜蔓具有神奇的性质, ...