2진수, 8진수, 16진수를 10진수로 변환



java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.ghj.blog_030;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
 
    //UI
    TextView txt2Num;
    TextView txt8Num;
    TextView txt16Num;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //UI
        txt2Num = (TextView)findViewById(R.id.txt2Num);
        txt8Num = (TextView)findViewById(R.id.txt8Num);
        txt16Num = (TextView)findViewById(R.id.txt16Num);
 
        //2진수=>10진수
        int bin2dec = Integer.parseInt("101010"2);
        txt2Num.setText(""+bin2dec);
 
        //2진수=>10진수
        int oct2dec = Integer.parseInt("47"8);
        txt8Num.setText(""+oct2dec);
 
        //2진수=>10진수
        int hex2dec = Integer.parseInt("a0f"16);
        txt16Num.setText(""+hex2dec);
    }
}
 
cs



xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="101010 (2진수) => "
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <TextView
            android:id="@+id/txt2Num"
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="47 (8진수) => "
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <TextView
            android:id="@+id/txt8Num"
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:text="a0f (16진수) => "
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
        <TextView
            android:id="@+id/txt16Num"
            android:textSize="24dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
 
</LinearLayout>
 
cs



결과


+ Recent posts