Bagi kita yang pernah belajar tentang jaringan, salah satu yang ada dalam jaringan adalah ip address. IP address tersebut sering kita hitung/konversi ke bentuk biner untuk mencari Network ID, broadcast ataupun menghitung subnetting melalui jaringan. Misal saja kita memiliki IP address : 192.168.10.100
Source Code Konversi IP Address ke biner 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | package com.kurungkurawal.utfbinary; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class UTFBinary extends JFrame { private final String APP_NAME = "UTF8 <=> Binary Converter"; public UTFBinary(){ super(); setTitle(APP_NAME); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setMinimumSize(new Dimension(300, 240)); buildGUI(); pack(); setLocationRelativeTo(null); setVisible(true); } private JTextArea txtUtf, txtBin; private void buildGUI(){ Border padd = BorderFactory.createEmptyBorder(5,5,5,5); JLabel title = new JLabel(APP_NAME); title.setHorizontalAlignment(SwingConstants.CENTER); title.setBorder(padd); getContentPane().add(title, BorderLayout.NORTH); txtUtf = new JTextArea(5,30); txtUtf.setBorder(padd); txtUtf.setLineWrap(true); txtBin = new JTextArea(5,30); txtBin.setBorder(padd); txtBin.setLineWrap(true); JPanel body = new JPanel(); body.setBorder(padd); body.setLayout(new BoxLayout(body, BoxLayout.X_AXIS)); body.add(new JScrollPane(txtUtf)); body.add(Box.createRigidArea(new Dimension(5,0))); body.add(new JScrollPane(txtBin)); getContentPane().add(body, BorderLayout.CENTER); txtBin.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent keyEvent) { convertFromBin(); } }); txtUtf.addKeyListener(new KeyAdapter() { @Override public void keyReleased(KeyEvent keyEvent) { convertFromUtf(); } }); } private void convertFromUtf(){ String utf = txtUtf.getText(); String bin = ""; try { byte[] bytes = utf.getBytes("UTF-8"); for (byte b : bytes) { bin += String.format("%8s", Integer.toBinaryString((int) b)).replace(' ', '0'); } } catch (Exception e){ e.printStackTrace(); } txtBin.setText(bin); } private void convertFromBin(){ String bin = txtBin.getText(); String utf = ""; String tmp; int itmp; for(int i = 0; i < bin.length() && bin.length() >= 8; i += 8){ try { tmp = bin.substring(i, i + 8); itmp = Integer.parseInt(tmp, 2); utf += new String(new byte[]{ (byte) itmp }, "utf-8"); } catch (Exception e){ break; } } txtUtf.setText(utf); } } |
ConversionConversion EmoticonEmoticon