public byte[] int2byte(int data){ byte b4 = (byte)((data)>>24); byte b3 = (byte)(((data)<<8)>>24); byte b2= (byte)(((data)<<16)>>24); byte b1 = (byte)(((data)<<24)>>24); byte[] bytes = {b1,b2,b3,b4}; return bytes; } public int byte2int(byte b[]) { int b3 = b[3] & 0xff; int b2 = b[2] & 0xff; int b1 = b[1] & 0xff; int b0 = b[0] & 0xff; int i = b3 << 24 | b2 << 16 | b1 << 8 | b0; return i; }
8个0或1组成的String与byte转换思路:
暂时想到的是String每个元素变为char,char-48变成byte,8个byte位移变成byte
下面是8个byte(0或1)位移变为1个byte的方法:
/** * 8个0或1的int转换为byte * * @return 8个0与1拼成的一个byte */ public byte arr2byte(byte arr[]) { byte b = 0; for (int i = 0; i < arr.length; i++) { b |= (byte) (arr[i] << 7 - i); } return b; }
将byte转为01串也是类似的,依次取出byte的第1到8为拼成串。
public String byte2String(byte b) { String s = ""; for (int i = 0; i < 8; i++) { byte temp = (byte) ((byte) (b >> 7 - i) & 1); s += temp; } return s; }
引用 人生难得糊涂的话解释为什么&1(纠结了一下午)
比如说2 的二进制是00000010
右移7位是00000000 结果为 0 是第八位
右移6位是00000000 结果为 0 是第7位
。。。。
右移1位是 00000001 结果为1 是第2位
右移0位是 00000010 我们要的结果是最后一个0 但是现在他的值是2 那怎么取到最后一个0呢 只要&一个1 就可以了
右移6位是00000000 结果为 0 是第7位
。。。。
右移1位是 00000001 结果为1 是第2位
右移0位是 00000010 我们要的结果是最后一个0 但是现在他的值是2 那怎么取到最后一个0呢 只要&一个1 就可以了
相关推荐
byte转化工具类,可以实现byte转int,数组,string,小端取高位,低位等
VC (CString,char,int,long,string,byte...)名种类形间的转换
TIA博途_S7-1200中将BYTE类型的数据转换成char类型的具体方法和注意事项
下面小编就为大家带来一篇java byte数组与int,long,short,byte的转换实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
包含MD5加密,同时拥有多种方式进行byte或者byte[]与16进制相互转换 三种方式: 方式一:Integer.toHexString(int) 对应的是: Integer.parseInt(String, int)或者Integer.valueOf(String, int) 方式二:位运算(& ...
在switch(expr1)中,expr1只能是一个整数表达式或者枚举常量(更大字体),整数表达式可以是int基本类型或Integer包装类型,由于,byte,short,char都可以隐含转换为int,所以,这些类型以及这些类型的包装类型也...
Java字节数组与常见类型转换工具,包括byte[] 与int long double float string boolean 等类型的相互转换。
介绍php字符串与byte字节数组转化类,转换一个String字符串为byte数组,将字节数组转化为String类型的数据,转换一个int为byte数组,从字节数组中指定的位置读取一个Integer类型的数据,转换一个shor字符串为byte...
2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) String类 String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) ...
这个类库可以实现 1.转换一个String字符串为...3.转换一个int为byte数组 4.从字节数组中指定的位置读取一个Integer类型的数据 5.转换一个shor字符串为byte数组 6.从字节数组中指定的位置读取一个Short类型的数据
bool __stdcall stringtohex(unsigned char *inbuf,int inlen,unsigned char * outbuf); bool __stdcall ishex(BYTE *buf,int bl); bool __stdcall isbcd(BYTE *buf,int bl); bool __stdcall encodebcd(BYTE *buf,...
1、将一个对象转换为byte对象 public static byte GetByte(object o) { byte retInt = 0; if (o != null) { ...public static byte GetByteFormHex(string hexValue) { try { return Convert.ToBy
C#,byte数组与int、string的转换,byte数组的拼接截取等。
图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath); MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); byte[] arr = new byte[ms.Length...
字符串转换: CString,int,string,char*之间的转换 ;
int转换为byte数组 byte数组转换为int 保留几位小数 null转String String转Byte String转Boolean String转Int String转Short String转Double Int转String Double转Long Double转Int Long转Double Long转Int String转...
public extern static IntPtr LoadLibrary(ref byte buf, int nLen); [DllImport("kernel.dll", EntryPoint = "MemGetProcAddress")] public extern static IntPtr GetProcAddress(IntPtr handle, String ...
一些初学JAVA的朋友可能会遇到JAVA的数据类型之间转换的苦恼,例如,整数和float,double型之间的转换,整数和String类型之间的转换,以及处理、显示时间方面的问下面笔者就开发中的一些体会介绍给大家。 我们知道,...
此文档主要的内容是byte类型与各类型(String、boolean、int、inputStream等)之间的转化
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别...