Android常用数据类型转换
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常用数据类型转换的更多相关文章
- Android 常用数据适配器SimpleAdapter
在<Android 常用数据适配器ArrayAdapter>中介绍了ArrayAdapter数据适配器.但是存在一个缺陷,那就是条目的图标都固定相同,要显示每个条目的图标都不相同,那么使用 ...
- Python基础学习笔记(九)常用数据类型转换函数
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-variable-types.html 3. http://www ...
- 泛型集合、datatable常用数据类型转换Json帮助类
泛型集合.datatable常用数据类型转换Json帮助类 using System; using System.Data; using System.Configuration; using Sys ...
- Android 常用数据适配器ArrayAdapter
接着上篇文章<Android 采用Layout Inflater创建一个View对象>,本文采用常用数据适配器ArrayAdapter 新建项目后,在layout文件夹下新建list_it ...
- Python常用数据类型转换
常用的数据类型转换 目标 了解类型转换的作用 掌握常用的类型转换 函数 说明 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ...
- python运算符和常用数据类型转换
运算符 算术运算符 运算符 描述 实例 + 加 两个对象相加 a + b 输出结果 30 - 减 得到负数或是一个数减去另一个数 a - b 输出结果 -10 * 乘 两个数相乘或是返回一个被重复若干 ...
- java常用数据类型转换
在Java开发过程中经常会涉及到数据类型的转换问题,比如数字型转字符型,字符型转日期型,字符串转数组等等,以及其他类型的强制转换等.经常出现,所以有必要总结一下. 1.如何将字串 String 转换成 ...
- Android 常用数据操作封装类案例
1.DbHelper类 继承自SQLiteOpenHelper类,实现对数据库的基本操作 package com.example.utils; import android.content.Conte ...
- 【Java】常用数据类型转换(BigDecimal、包装类、日期等)
新工作转到大数据方向,每天都要面对数据类型互相转换的工作,再加上先前面试发现这部分的知识盲点, 决定复习之余自己再写一套便捷的方法,以后会比较方便.(虽然公司有现成封装的类,里头还有些遗漏的地方,暂时 ...
随机推荐
- Go标准库之读写文件(File)
Go标准库之读写文件(File) 创建一个空文件 package main import ( "log" "os" ) func main() { file, ...
- 没有安装hiredis
在redis的发行包中的deps目录中就包含hiredis的源码,手动编译安装,或者自行下载一份.地址:hiredis的地址 cd /deps/hiredis make make install 然后 ...
- oracle查看当前用户,数据库实例
#sysdba用户登录[oracle@oracle ~]$ sqlplus / as sysdba #查看当前用户sql>show user; #查看当前数据库实例sql>show par ...
- springboot shiro开启注释
shiroconfiguration中增加 @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceA ...
- Vue + Element UI 实现权限管理系统 前端篇(十六):系统备份还原
系统备份还原 在很多时候,我们需要系统数据进行备份还原.我们这里就使用MySql的备份还原命令实现系统备份还原的功能. 后台接口准备 系统备份还原是对数据库的备份还原,所以必须有后台接口的支持,我们准 ...
- MongoDB 备份与还原 mongodump、mongorestore
目录 MongoDB 备份与还原 一. MongoDB 备份 1.mongodump 2 .cp 或者rsync 3.单节点意外关闭后,如何恢复数据 4.查看备份数据 二.MongoDB 还原 1.m ...
- leetcode — regular-expression-matching
/** * Source : https://oj.leetcode.com/problems/regular-expression-matching/ * * Created by lverpeng ...
- Linux下rsync daemon模式下的错误汇总
一.前言:最近学习服务环境搭建,遇到了许多大大小小的问题,不过还好,经过我的一通努力终于都解决了,所以分享出来给自己留个纪念,同时也希望能帮助学习中的朋友. 二.环境:两台服务器环境相同 1 [roo ...
- Windows系统环境下创建mysql主从数据库方法(双向主从复制)
创建mysql主从数据库方法(双向主从复制) (一)Windows系统下的MySQL主从复制(单向复制) (1)环境说明: 1,Mysql版本:mysql5.7.20(主从机mysql版本必须一致) ...
- jquery插入,复制、替换和删除节点
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...