10진수를 2진수, 8진수, 16진수로 변환
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package com.ghj.blog_028; import android.net.ParseException; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends AppCompatActivity { //UI EditText editNumber; TextView txt2Num; TextView txt8Num; TextView txt16Num; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //UI editNumber = (EditText)findViewById(R.id.editNumber); txt2Num = (TextView)findViewById(R.id.txt2Num); txt8Num = (TextView)findViewById(R.id.txt8Num); txt16Num = (TextView)findViewById(R.id.txt16Num); } //진법변환 public void mOnTransfer(View v){ String strnum = editNumber.getText().toString(); int num = -1; try{ num = Integer.parseInt(strnum); //10진수 -> 2진수 String str2num = Integer.toBinaryString(num); txt2Num.setText(str2num); //10진수 -> 8진수 String str8num = Integer.toOctalString(num); txt8Num.setText(str8num); //10진수 -> 16진수 String str16num = Integer.toHexString(num); txt16Num.setText(str16num); }catch(ParseException e){ } } } | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <?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"> <EditText android:id="@+id/editNumber" android:inputType="numberDecimal" android:layout_weight="4" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:text="변환" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:onClick="mOnTransfer"/> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="2진수 : " android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <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="8진수 : " android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <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="16진수 : " android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt16Num" android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> | cs |
결과
'IT > - 프로그래밍' 카테고리의 다른 글
안드로이드 앱 최초 실행여부 확인하기 (1) | 2016.11.13 |
---|---|
안드로이드 2진수, 8진수, 16진수를 10진수로 변환 (0) | 2016.11.13 |
안드로이드 와이파이 상태, 네트워크 상태 수신 (0) | 2016.11.12 |
안드로이드 MCC(Mobile Country Code) MNC(Mobile Network Code) 구하기 (0) | 2016.11.08 |
안드로이드 블루투스 페어링 하기 (마스터 모드) (9) | 2016.11.06 |