A. java 16進制字元串轉為二進制bit數組
應該用byte型數組
public static String stringToHexString(String strPart) {
String hexString = "";
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString = hexString + strHex;
}
return hexString;
}
private static String hexString="0123456789ABCDEF";
/*
* 將字元串編碼成16進制數字,適用於所有字元(包括中文)
*/
public static String encode(String str)
{
// 根據默認編碼獲取位元組數組
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
// 將位元組數組中每個位元組拆解成2位16進制整數
for(int i=0;i<bytes.length;i++)
{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* 將16進制數字解碼成字元串,適用於所有字元(包括中文)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
// 將每2位16進制整數組裝成一個位元組
for(int i=0;i<bytes.length();i+=2)
baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String(baos.toByteArray());
}
private static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] {src1})).byteValue();
byte ret = (byte) (_b0 | _b1);
return ret;
public static byte[] HexString2Bytes(String src)
{
byte[] ret = new byte[6];
byte[] tmp = src.getBytes();
for(int i=0; i<6; ++i )
{
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}
B. java中十六進制怎麼轉換為2進制
java十六進制轉換為2進制示例如下:
publicclassHex2Binary
{
publicstaticvoidmain(String[]args)
{
StringhexString="ABCD";
System.out.println(hexString2binaryString(hexString));
}
(StringhexString)
{
if(hexString==null||hexString.length()%2!=0)
returnnull;
StringbString="",tmp;
for(inti=0;i<hexString.length();i++)
{
tmp="0000"
+Integer.toBinaryString(Integer.parseInt(hexString
.substring(i,i+1),16));
bString+=tmp.substring(tmp.length()-4);
}
returnbString;
}
}
C. 急,急急,跪求java十六進制轉換成二進制(要自己寫演算法),再把得到的二進制數取反後,在轉換成十六進制
Java程序:
public class Test29 {
public static void main(String[] args) {
String hex = "12345abcdef67890";
String bin;
bin = Transform.convertHexToBin(hex);
System.out.println(hex + " --> " + bin);
hex = Transform.convertBinToHex(bin);
System.out.println(bin + " --> " + hex);
}
}
class Transform{
private static String[] hexs = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f"};
private static String[] bins = new String[]{"0000", "0001", "0010", "0011", "0100", "0101",
"0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
//將十進制數hex轉換為二進制數並返回
public static String convertHexToBin(String hex){
StringBuffer buff = new StringBuffer();
int i;
for(i=0; i<hex.length(); i++){
buff.append(getBin(hex.substring(i,i+1)));
}
return buff.toString();
}
//將二進制數bin轉換為十六進制數並返回
public static String convertBinToHex(String bin){
StringBuffer buff = new StringBuffer(bin);
int i;
if(bin.length()%4 != 0){//左補零
for(i=0; i<(4-bin.length()%4); i++){
buff.insert(0, "0");
}
}
bin = buff.toString();
buff = new StringBuffer();
for(i=0; i<bin.length(); i+=4){
buff.append(getHex(bin.substring(i,i+4)));
}
return buff.toString();
}
//返回十六進制數的二進制形式
private static String getBin(String hex){
int i;
for(i=0; i<hexs.length && !hex.toLowerCase().equals(hexs[i]); i++);
return bins[i];
}
//返回二進制數的十六進制形式
private static String getHex(String bin){
int i;
for(i=0; i<bins.length && !bin.equals(bins[i]); i++);
return hexs[i];
}
}
運行測試:
12345abcdef67890 -->
--> 12345abcdef67890
D. 如何用JAVA語言將十六進制數轉換成二進制數,並輸出
我的思路很簡單:
publicclass六轉2
{
publicstaticvoidmain(String[]args)
{
System.out.println(" ==========16轉二進制========== ");
init();
}//初始化!
privatestaticvoidinit()
{
//字元串形式的:16進制!
Strings="ACCC";
//字元串形式十進制--作為橋梁!
intsint=Integer.valueOf(s,16);
//十進制在轉換成二進制的字元串形式輸出!
Stringbin=Integer.toBinaryString(sint);
//輸出!
System.out.println(bin);
}
}
E. 急!!!!!!!!!!!!!~~~~~~~~~~~~~求2進制轉16進制的java程序
有現成的,你告訴我郵箱,我發給你
已經發到你郵箱,請查收,100426,15:39
F. Java怎麼用整數轉換二進制跟16進制,方法簡單點,謝謝
這太簡單了
toBinaryString(int i)
以二進制(基數 2)無符號整數形式返回一個整數參數的字元串表示形式。
toHexString(int i)
以十六進制(基數 16)無符號整數形式返回一個整數參數的字元串表示形式。
直接調用這兩個方法就可以了。
G. 二進制轉換成16進制 java string
如果使用jdk提供的方法,示例代碼如下:
String str="11111111"; //要轉化的二進制碼
int i=Integer.parseInt(str,2); //轉成10進制
String hex=Integer.toHexString(i); //轉成16進制
System.out.println(hex);
H. Java 二進制轉換16進制
import java.util.Scanner;
public class Binary2Hex {
private static String hexStr = "0123456789ABCDEF";
/**
*
* @param bytes
* @return 將二進制轉換為十六進制字元輸出
*/
public static String BinaryToHexString(byte[] bytes){
String result = "";
String hex = "";
for(int i=0;i<bytes.length;i++){
//位元組高4位
hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));
//位元組低4位
hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));
result +=hex;
}
return result;
}
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.print("請輸入一個二進制字元串:");
String str = sca.next();
System.out.println();
System.out.println("您輸入的是字元串的十六進制為:"+BinaryToHexString(str.getBytes()));
System.out.print("是否繼續轉化(如果不繼續請輸入「exit」,繼續的話輸入任意字元):");
String _str = sca.next();
if (_str.equalsIgnoreCase("exit")) {
flag = false;
}
}
}
}
I. JAVA,簡單實現16進制轉2進制的代碼問題
16進制轉2機制,可以直接轉不用先轉10進制
暫且按整形計算,四個位元組
int a=0x10;
byte[] byte=new byte[4]
for(int i=0;i<byte.length;i++){
byte[i]=(a>i*8)&0xFF;//一個位元組8位
}
這個byte就是二進制