[英]Convert string binary to hexadecimal
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
int digitNumber=1;
int sum = 0;
String binary = "1110101011111010";
String hex;
for(int i = 0; i < binary.length(); i++)
{
if(digitNumber == 1)
sum += Integer.parseInt(binary.charAt(i) + "")*128;
else if (digitNumber == 2)
sum += Integer.parseInt(binary.charAt(i) + "")*64;
else if (digitNumber == 3)
sum += Integer.parseInt(binary.charAt(i) + "")*32;
else if (digitNumber == 4)
sum += Integer.parseInt(binary.charAt(i) + "")*16;
else if (digitNumber == 5)
sum += Integer.parseInt(binary.charAt(i) + "")*8;
else if (digitNumber == 6)
sum += Integer.parseInt(binary.charAt(i) + "")*4;
else if (digitNumber == 7)
sum += Integer.parseInt(binary.charAt(i) + "")*2;
else if (digitNumber == 8)
{
sum += Integer.parseInt(binary.charAt(i) + "")*1;
hex = Integer.toString(sum,16);
System.out.print(hex);
}
else if (digitNumber == 9)
{
digitNumber = 1;
sum=0;
}
digitNumber++;
}
}
}
大家好,我正在嘗試將二進制字符串轉換為十六進制。 我的字符串二進制是“1110101011111010”。 輸出應該是EAFA,但我的輸出是EA7A。 我的代碼有什么問題? 有人可以幫我嗎?
我覺得你的代碼太多了。
嘗試這個:
String hex = Long.toHexString(Long.parseLong(binary, 2)); // handles up to 63 bits
請記住:您擁有的代碼越多,隱藏錯誤的地方就越多。
您代碼中的實際問題是,在digitNumber == 9
的情況下,您將digitNumber
設置為 1 然后將其遞增,但您不解析該數字,因此您跳過了第二個字節的第一位重新解析。
推薦的方法是切換到使用Integer.parseInt(binary, 2)
, Long.parseLong(binary, 2)
甚至new BigInteger(binary, 2)
。
但是,如果您想修復您的代碼,您需要將digitNumber == 9
的檢查移動到循環的開頭,並且獨立於其他 if 語句,因此:
for(int i = 0; i < binary.length(); i++) {
if (digitNumber == 9) {
digitNumber = 1;
sum=0;
}
if(digitNumber == 1)
sum += Integer.parseInt(binary.charAt(i) + "")*128;
else
// rest of your if...
digitNumber++;
}
如果您需要一個長string
,您可以使用BigInteger
將任何二進制字符串轉換為十六進制。
public static String convertBinaryToHexadecimal(String binaryStr) {
return new BigInteger(binaryStr, 2).toString(16);
}
輸出:
BigInteger num = BigInteger.valueOf(Long.MAX_VALUE);
String binaryStr = num.add(num).toString(2); // 2 times bigger than long
System.out.println(convertBinaryToHexadecimal(binaryStr)); // fffffffffffffffe
public static String convertHexadecimalToBinary(String hexadecimalStr, int length) {
return String.format("%0" + length + 'd', new BigInteger(new BigInteger(hexadecimalStr, 16).toString(2)));
}
輸出:
String hexadecimalStr = "7B";
System.out.println(convertHexadecimalToBinary(hexadecimalStr, 8)); // 01111011
使用@Bohemian♦ 所做的內置函數當然是最簡單的。 但是,如果您確實需要/想要手動進行二進制轉換,則可以將for
循環更改for
以下內容:
for (int i = 0; i < binary.length(); i++) {
sum += Integer.parseInt(binary.charAt(binary.length() - i - 1) + "") * (1 << i);
}
這會從右到左循環遍歷二進制字符串。 1 << i
等價於Math.pow(2, i)
。
我使用了將字符串二進制轉換為十六進制的基本方法,請在下面找到:
public class StringToHexaDecmal {
// Driver program to test above function
public static void main(String[] args) {
String num = new String("1110101011111010");
int decValue = binaryToDecimal(num);
stringToHexadecimal(decValue);
}
// Function to convert binary to decimal
static int binaryToDecimal(String n) {
String num = n;
int dec_value = 0;
// Initializing base value to 1,
// i.e 2^0
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num.charAt(i) == '1')
dec_value += base;
base = base * 2;
}
return dec_value;
}
// Function to convert decimal value to Hexadecimal
private static void stringToHexadecimal(int no) {
// TODO Auto-generated method stub
String hexadecimalNo = "";
char c;
while (no != 0) {
int rem = no % 16;
if (rem > 9 && rem <= 15) {
c = (char)(rem + 55);
hexadecimalNo = c + hexadecimalNo;
}
if (rem < 10) {
hexadecimalNo = rem + hexadecimalNo;
}
no = no / 16;
}
System.out.println(hexadecimalNo);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.