java案例實戰(zhàn)之字符串轉換為二進制
任務描述
本例要求編寫一個程序,從鍵盤錄入一個字符串,將字符串轉換為二進制數。在轉換時,將字符串中的每個字符單獨轉換為一個二進制數,將所有二進制數連接起來進行輸出。
案例在實現時,要求使用Math類、String類以及Scanner等常見Java API的常用方法.
運行結果
案例任務
- 學會分析“十進制轉化成二進制”任務的實現思路。
- 根據思路獨立完成“十進制轉化成二進制”任務的源代碼編寫、編譯及運行。
- 掌握String類、Math類及Scanner類中常用方法的使用。
- 掌握之前學習的雙重for循環(huán)以及數組的相關知識。
案例思路
(1) 分析任務描述可知,鍵盤錄入要轉化的字符串。用Scanner實現。
(2)定義一個二維數組。其中4代表每一行長度。ss.length()根據鍵盤錄入的字符串表示有多少行。
(3)利用for循環(huán)遍歷字符串,遍歷后用String的charAt()方法獲取每個字符并轉化成int。char與int進行運算,char的數值要減去 48,因為ASCII碼中0的值是48,1就是49。
(4)轉化成int后再用for循環(huán)獲取每一個int類型的數進行轉化成二進制賦值給數組。其中Math.pow()方法是Math類中求冪的方法。
(5)最后用雙重for循環(huán)遍歷二維數組。將結果輸出到控制臺。
案例實現
//鍵盤錄入要轉化的字符串。用Scanner實現。 Scanner sc = new Scanner(System.in); System.out.println("請輸入要轉換的字符串:"); String ss = sc.nextLine(); //定義一個二維數組。其中4代表每一行長度。ss.length()根據鍵盤錄入的字符串表示有多少行。 int [][] arr = new int[ss.length()][4]; //利用for循環(huán)遍歷字符串,遍歷后用String的charAt()方法獲取每個字符并轉化成int。char與int進行運算,char的數值要減去 48,因為ASCII碼中0的值是48,1就是49。 for (int i = 0; i < ss.length(); i++) { int charss = (int) ss.charAt(i)-48; for (int j = 0; j < 4; j++) { //轉化成int后再用for循環(huán)獲取每一個int類型的數進行轉化成二進制賦值給數組。其中Math.pow()方法是Math類中求冪的方法。 arr[i][j] = (int)((charss/Math.pow(2, 3-j))%2); } } // 最后用雙重for循環(huán)遍歷二維數組。將結果輸出到控制臺。 System.out.println("二進制的數是:"); // 最后用雙重for循環(huán)遍歷二維數組。將結果輸出到控制臺。 for (int i = 0; i < ss.length(); i++) { for (int j = 0; j < 4; j++) { System.out.print(arr[i][j]); } }
補充:Java中String與二進制碼的相互轉換
把字符串轉成二進制碼
public class stringTest { public static void main(String[] args) { String str = "abc"; String binary = toBinary(str); System.out.println(binary); } public static String toBinary(String str){ //把字符串轉成字符數組 char[] strChar=str.toCharArray(); String result=""; for(int i=0;i<strChar.length;i++){ //toBinaryString(int i)返回變量的二進制表示的字符串 //toHexString(int i) 八進制 //toOctalString(int i) 十六進制 result +=Integer.toBinaryString(strChar[i])+ " "; } return result; } }
結果:abc–>1100001 1100010 1100011
將二進制碼轉成字符串
public class stringTest { public static void main(String[] args) { String binary = "1100001 1100010 1100011"; System.out.println(toString(binary)); } public static String toString(String binary) { String[] tempStr=binary.split(" "); char[] tempChar=new char[tempStr.length]; for(int i=0;i<tempStr.length;i++) { tempChar[i]=BinstrToChar(tempStr[i]); } return String.valueOf(tempChar); } //將二進制字符串轉換成int數組 public static int[] BinstrToIntArray(String binStr) { char[] temp=binStr.toCharArray(); int[] result=new int[temp.length]; for(int i=0;i<temp.length;i++) { result[i]=temp[i]-48; } return result; } //將二進制轉換成字符 public static char BinstrToChar(String binStr){ int[] temp=BinstrToIntArray(binStr); int sum=0; for(int i=0; i<temp.length;i++){ sum +=temp[temp.length-1-i]<<i; } return (char)sum; } }
結果:1100001 1100010 1100011 –>abc
總結
到此這篇關于java字符串轉換為二進制的文章就介紹到這了,更多相關java字符串轉換為二進制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決Hibernate4執(zhí)行save()或update()無效問題的方法
這篇文章主要為大家詳細介紹了解決Hibernate4執(zhí)行save()或update()無效問題的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06java8 LocalDate LocalDateTime等時間類用法實例分析
這篇文章主要介紹了java8 LocalDate LocalDateTime等時間類用法,結合具體實例形式分析了LocalDate、LocalTime、LocalDateTime等日期時間相關類的功能與具體使用技巧,需要的朋友可以參考下2017-04-04