string和int之間的轉(zhuǎn)換?
字符串轉(zhuǎn)換成數(shù)據(jù)
String MyNumber ="1234"; int MyInt = Integer.parseInt(MyNumber); 字符串轉(zhuǎn)換成byte, short, int, float, double, long等數(shù)據(jù)類型,可以分別參考Byte, Short, Integer, Float, Double, Long類的parseXXX 方法,。
a1=Integer.parseInt(s1); s1=Integer.toString(a1); 數(shù)據(jù)轉(zhuǎn)換成字符串
int MyInt = 1234; String MyString = "" + MyInt; 其它數(shù)據(jù)類型可以利用同樣的方法轉(zhuǎn)換成字符串,。 十進(jìn)制到其他進(jìn)制的轉(zhuǎn)換 十進(jìn)制整數(shù)轉(zhuǎn)換成二進(jìn)制整數(shù),返回結(jié)果是一個(gè)字符串: Integer.toBinaryString(int i); Integer和Long提供了toBinaryString, toHexString和toOctalString方法,,可以方便的將數(shù)據(jù)轉(zhuǎn)換成二進(jìn)制,、十六進(jìn)制和八進(jìn)制字符串。功能更加強(qiáng)大的是其toString(int/long i, int radix)方法,,可以將一個(gè)十進(jìn)制數(shù)轉(zhuǎn)換成任意進(jìn)制的字符串形式,。 byte, short, float和double等數(shù)據(jù)類型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法轉(zhuǎn)換成其他進(jìn)制的字符串形式,。 其它進(jìn)制到十進(jìn)制的轉(zhuǎn)換 五進(jìn)制字符串14414轉(zhuǎn)換成十進(jìn)制整數(shù),,結(jié)果是1234: System.out.println(Integer.valueOf("14414", 5); Integer和Long提供的valueOf(String source, int radix)方法,可以 將任意進(jìn)制的字符串轉(zhuǎn)換成十進(jìn)制數(shù)據(jù),。 把String類型轉(zhuǎn)換成16進(jìn)制的整數(shù)
public static void main(String args[]){ String x = "0x300C8"; int y = Integer.decode(x).intvalue(); System.out.println(y); } int,、char、double與byte相互轉(zhuǎn)換的程序 整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換
public static byte[] intToByte(int number) { int temp = number; byte[] b=new byte[4]; for (int i=b.length-1;i>-1;i--){ b[i] = new Integer(temp&0xff).byteValue(); //將最高位保存在最低位 temp = temp >> 8; //向右移8位 } return b; } 字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換
public static int byteToInt(byte[] b) { int s = 0; for (int i = 0; i < 3; i++) { if (b[i] >= 0) s = s + b[i]; else s = s + 256 + b[i]; s = s * 256; } if (b[3] >= 0) //最后一個(gè)之所以不乘,,是因?yàn)榭赡軙?huì)溢出 s = s + b[3]; else s = s + 256 + b[3]; return s; } 短整數(shù)與字節(jié)數(shù)組之間的相互轉(zhuǎn)換 short與int之間的區(qū)別在于short是兩個(gè)字節(jié)的,而int是四個(gè)字節(jié)的,。因此,,只需要將5 與6 中的范例程序小做改動(dòng),即可實(shí)現(xiàn)短整數(shù)與字節(jié)數(shù)組之間的相互轉(zhuǎn)換。 字符到字節(jié)轉(zhuǎn)換
public static byte[] charToByte(char ch){ int temp=(int)ch; byte[] b=new byte[2]; for (int i=b.length-1;i>-1;i--){ b[i] = new Integer(temp&0xff).bytevalue(); //將最高位保存在最低位 temp = temp >> 8; //向右移8位 } return b; } //字節(jié)到字符轉(zhuǎn)換
public static char byteToChar(byte[] b){ int s=0; if(b[0]>0) s+=b[0]; else s+=256+b[0]; s*=256; if(b[1]>0) s+=b[1]; else s+=256+b[1]; char ch=(char)s; return ch; } 浮點(diǎn)到字節(jié)轉(zhuǎn)換
public static byte[] doubleToByte(double d){ byte[] b=new byte[8]; long l=Double.doubleToLongBits(d); for(int i=0;i<b.length;i++){ b[i]=new Long(l).bytevalue(); l=l>>8; } return b; } 字節(jié)到浮點(diǎn)轉(zhuǎn)換
public static double byteToDouble(byte[] b){ long l; l=b[0]; l&=0xff; l|=((long)b[1]<<8); l&=0xffff; l|=((long)b[2]<<16); l&=0xffffff; l|=((long)b[3]<<24); l&=0xffffffffl; l|=((long)b[4]<<32); l&=0xffffffffffl; l|=((long)b[5]<<40); l&=0xffffffffffffl; l|=((long)b[6]<<48); l|=((long)b[7]<<56); return Double.longBitsToDouble(l); } int與byte array之間的轉(zhuǎn)換程序 在通訊中經(jīng)常需要將數(shù)值轉(zhuǎn)換成字節(jié)流,,或者是將字節(jié)流轉(zhuǎn)換成數(shù)值,。下面 提供的程序可以進(jìn)行int和byte array之間的轉(zhuǎn)換。
/** * * IntConverter * * This class provides methods to convert int into byte array and * byte array back into int. * */ public class IntConverter { /** * * Method converting int into byte array. * * @param number The int value to be converted. * */ public static byte[] toByteArray(int number) { int temp = number; byte[] b=new byte[4]; for (int i = b.length - 1; i > -1; i--) { b[i] = new Integer(temp & 0xff).bytevalue(); temp = temp >> 8; } return b; } /** * * Method converting byte array into int. * * @param The byte array to be converted. * */ public static int toInteger(byte[] b) { int s = 0; for (int i = 0; i < 3; i++) { if (b[i] > 0) s = s + b[i]; else s = s + 256 + b[i]; s = s * 256; } if (b[3] > 0) s = s + b[3]; else s = s + 256 + b[3]; return s; } // Testing program. public static void main(String[] args) { IntConverter abc = new IntConverter(); int s = -1121115678; byte[] b = abc.toByteArray(s); for (int i = 0; i <= 3; i++) System.out.println(b[i]); s = abc.toInteger(b); System.out.println(s); } } 字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換
public static int byteToint(byte[] convertByteValue) { byte[] YY=new byte[4]; YY=convertByteValue; int ee, ff, gg, hh; ee = YY[3] & 0x000000ff; //System.out.println("ee: " +ee); ff = (YY[2]<<8) & 0x0000ff00; //System.out.println("ff: " +ff); gg = (YY[1]<<16) & 0x00ff0000; // System.out.println("gg: " +gg); hh = YY[0]<<24; // System.out.println("hh: "+hh); int jj = ee + ff + gg + hh; // System.out.println("jj: "+jj); return jj; } 整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換
|
|