Today: Yesterday: Total: Online:
カテゴリー
その他
  • RSS1.0
  • RSS2.0
  • atom0.3
  • valid XHTML1.0
  • valid CSS2
  • Credit
RSSリーダーで購読する | この日記のはてなブックマーク数 | プロフィール
コメントが一番多い記事(233コメント):人の心理の裏をかくホームページ集客術:リピータになってもらうためには
ツイートが一番多い記事(94ツイート):ちょっと待て!twitterやfacebookに子供の写真を掲載する親達への警告
いいねが一番多い記事(574いいね):facebookスパムに要注意。なりすましの見分け方とアカウント乗っ取りの手口(2013年度版)

カテゴリー » JAVA November 06, 2006

型変換・キャスト ID:1162789292


●文字列から数値型への変換
int型 int a=Integer.valueOf(str).intValue()
int a=Integer.parseInt(str);
short型 short a=Short.valueOf(str).shortValue()
short a=Short.parseShort(str)
byte型 byte a=Byte.valueOf(str).byteValue()
byte a=Byte.parseByte(str)
long型 long a=Long.valueOf(str).longValue()
long a=Long.parseLong(str);
float型 float a=Float.valueOf(str).floatValue()
float a=Float.parseFloat(str);
double型 double a=Double.valueOf(str).doubleValue()
double a=Double.parseDouble(str);

●数値から文字列への変換
int型 String s1=String.valueOf(intvalue)
short型 String s1=String.valueOf(shortvalue)
byte型 String s1=String.valueOf(bytevalue)
long型 String s1=String.valueOf(longvalue)
float型 String s1=String.valueOf(floatvalue)
double型 String s1=String.valueOf(doublevalue)

●float→int 型変換
int型への変換 float a=(float)1.9;
float b=(float)-1.9;
int c=(int)a;
int d=(int)b;
c=1,d=-1 となる。

●文字列のbyte型への変換
byte型への変換 String s = "123456";
byte b[] = s.getBytes();


●16進、8進、2進数の変換
Source int a = 100;
String s1 = Integer.toBinaryString(a);
String s2 = Integer.toOctalString(a);
String s3 = Integer.toHexString(a);
System.out.println("S1=" + s1 + " s2=" + s2 + " s3=" + s3);

String s4 = "0xFA";
int b = Integer.decode(s4).intValue();
System.out.println("b=" + b);

String s5 = "FF";
int c = Integer.parseInt(s5,16);
System.out.println("c=" + c);

String s6 = "10001";
int d = Integer.parseInt(s6,2);
System.out.println("d=" + d);

int e=-10;
System.out.println("e=" + Integer.toHexString(e));
Output S1=1100100 s2=144 s3=64
b=250
c=255
d=17
e=fffffff6
16進文字8桁で表現された数値をINTに変換
Source private static int convInt(String strInt){
if (Integer.parseInt(strInt.substring(0,1),16) < 8){
return Integer.parseInt(strInt,16);
}
String strSub = strInt.substring(1,8);
if (strInt.substring(0,1).equals("F")){
return -2147483648 + Integer.parseInt("7" + strSub,16);
}
if (strInt.substring(0,1).equals("E")){
return -2147483648 + Integer.parseInt("6" + strSub,16);
}
if (strInt.substring(0,1).equals("D")){
return -2147483648 + Integer.parseInt("5" + strSub,16);
}
if (strInt.substring(0,1).equals("C")){
return -2147483648 + Integer.parseInt("4" + strSub,16);
}
if (strInt.substring(0,1).equals("B")){
return -2147483648 + Integer.parseInt("3" + strSub,16);
}
if (strInt.substrin0,1).equals("A")){
return -2147483648 + Integer.parseInt("2" + strSub,16);
}
if (strInt.substring(0,1).equals("9")){
return -2147483648 + Integer.parseInt("1" + strSub,16);
}
if (strInt.substring(0,1).equals("8")){
return -2147483648 + Integer.parseInt("0" + strSub,16);
}
return 0;
}

2byteデータをintに変換
(括弧を使用して式の演算順序を明示的に指定する必要がある。)
Source int b;
byte a[] = new byte[2];
a[0]=(byte)0x80; a[1]=(byte)0xFF;
b=(a[0] <<8) + ((a[1])&0xFF);
System.out.println(b);

b=((a[0]<<8)&0xFF00) + ((a[1])&0xFF);
System.out.println(b);
Output -32513
33023

●浮動小数点関連
浮動小数点形式(IEEE754) float

double


floatをHEXに変換 float a;
a=(float)12.5;
String Hex=Integer.toHexString(Float.floatToRawIntBits(a));
System.out.println(Hex);

変換結果 a=12.5 41480000
a=3 40400000
a=2 40000000
a=1 3F800000
a=0 00000000
a=-1 BF800000

HEXをfloatに変換 String strFloat = "3F800000";
float a;
a = Float.intBitsToFloat((int)Long.parseLong(strFloat,16));


http://www.viste.com/Java/Language/language3.html
1 / 2| 次のページ »
— posted by midilin @ 02:01PM
[コメントが多かった順の記事]
人の心理の裏をかくホームページ集客術:リピータになってもらうためには(233)
ホームページ心理学第2弾「メールの書きだしとホームページ運営・集客の関係」(76)
娘が生まれた(69)
ゲームと言えば、何を思い出す? (50)
Microsoftからアクセス (30)
「目標の立て方」と「努力」と「実行力」(26)