我需要将给定的整数(我的整数总是小于30000)转换为相应的十六进制值,然后获取该值的字符。输出的长度应为2个字节。
Ex1:
Ex2:
以下C ++代码适用于上述两种情况。
void WriteBinaryTag(char* _zBuf, int _iValue)
{
unsigned short usValue = (unsigned short)_iValue;
((unsigned char*)_zBuf)[1] = (unsigned char)usValue & 0x00FF;
usValue >>= 8;
((unsigned char*)_zBuf)[0] = (unsigned char)usValue & 0x00FF;
}
现在我需要在Java中做同样的事情,我用以下方式做到了。
public static char[] writeBinaryTag (int iTag) {
char[] data = new char[2];
data[1]= (char) (iTag & 0x00FF);
iTag >>= 8;
data[0]= (char) (iTag & 0x00FF);
return data;
}
问题: 我测试了上面的java方法如下。
char[] temp = writeBinaryTag(207);
StringBuilder frame = new StringBuilder();
frame.append(temp);
System.out.println("OUT1:"+207+":"+frame.toString()+"|");
char[] temp2 = writeBinaryTag(25135);
StringBuilder frame2 = new StringBuilder();
frame2.append(temp2);
System.out.println("OUT2:"+25135+":"+frame2.toString()+"|");
使用StringBuilder,因为我需要将几个变量附加到除上述二进制标记之外的相同帧。最后,我打印上面的帧值。然后我使用binaryviewer分析打印的日志文件,以查看相应的十六进制值。 问题是上面的java代码仅适用于上面的示例1(值25135)。对于示例二(值207),它产生:
有人可以帮助我弄清楚java代码的问题。
答案 0 :(得分:2)
你不是在这里的任何地方转换为十六进制。您似乎想要将16位值转换为两个字节。 (a char
是Java中的16位值)
等价物是
void writeBinaryTag(byte[] zBuf, int _iValue) {
zBuf[1] = (byte) _iValue;
zBuf[0] = (byte) (_iValue >> 8);
}
创建一个byte []只是为了做到这一点是效率低下的,而且几乎肯定不是你想写的,而是有一个API可以做到这一点。
ByteBuffer bb = ....order(ByteOrder.LITTLE_ENDIAN);
bb.writeShort((short) _iValue);
// later to read an unsigned short
int iValue = bb.readShort() & 0xFFFF;
答案 1 :(得分:1)
看到我正在使用一些硬件交互,当时我在util中编写了不同的方法。它也有助于所有不同的转换。
private static String binaryToHex(String bin) {
return String.format("%X", Long.parseLong(bin,2)) ;
}
public static String hexToBinary16Bit(String hex16bit) {
int i = Integer.parseInt(hex16bit, 16);
String bin = toBinary(i);
return String.format("%0d", Integer.parseInt(bin));
}
private static String hexToBinary(String hex) {
int i = Integer.parseInt(hex, 16);
String bin = Integer.toBinaryString(i);
return String.format("%08d", Integer.parseInt(bin));
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
private static String hexBin(String hex) {
int a = Integer.parseInt(hex, 16);
System.out.println(a);
String finalBin = null;
int bit=1;
for(int i=0; i<16; i++) {
sBuilder.append((((a&bit)==0)?0:1));
finalBin = sBuilder.toString();
bit*=2;
}
return finalBin;
}
public static String[] customSplit(String src) {
String [] output = src.replaceAll("\\[","").replaceAll(" \\]","").split(",");
for (String element : output) {
System.out.println(element);
}
return output;
}
private static String hexToASCII(String hexValue) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < hexValue.length(); i += 2) {
String str = hexValue.substring(i, i + 2);
output.append((char) Integer.parseInt(str, 16));
}
return output.toString();
}
public static String hexBin8bit(String hex) {
StringBuilder sBuilder = new StringBuilder();
int a = Integer.parseInt(hex, 16);
System.out.println(a);
String finalBin = null;
int bit=1;
for(int i=0; i<8; i++) {
sBuilder.append((((a&bit)==0)?0:1));
finalBin = sBuilder.toString();
bit*=2;
}
return finalBin;
}
public static int toInt32(byte[] bytes, int index) throws Exception {
if (bytes.length != 4)
throw new Exception("The length of the byte array must be at least 4 bytes long.");
return (int) ((int) (0xff & bytes[index]) << 56 | (int) (0xff & bytes[index + 1]) << 48 | (int) (0xff & bytes[index + 2]) << 40 | (int) (0xff & bytes[index + 3]) << 32);
}
public static byte [] [] split(byte [] arrayIn,int len){
if (arrayIn == null) {
return null;
}
boolean even = arrayIn.length % len == 0;
int totalLength = arrayIn.length / len;
if (!even)
totalLength++;
byte[][] newArray = new byte[totalLength][];
for (int i = 0; i < totalLength; ++i) {
int allocLength = len;
if (!even && i == totalLength - 1)
allocLength = arrayIn.length % len;
newArray[i] = new byte[allocLength];
System.arraycopy(arrayIn, i * len, newArray[i], 0, allocLength);
}
return newArray;
}
public static byte[][] split1dArray1(byte[] arr1D, int columnSize) {
byte[][] arr2D = null;
int rowSize = arr1D.length/columnSize;
for(int i = 0; i < rowSize; i++){
for(int j = 0; j < columnSize; j++){
arr2D[i][j] = arr1D[i+j];
}
}
return arr2D;
public static byte[][] split1dArray2(byte[] arr1D, int columnSize) {
int len=arr1D.length;
if(len%512 !=0) {
len= len/columnSize +1;
} else {
len=len/columnSize;
}
byte [][] arr2D= new byte [len][columnSize];
int k=0;
for(int i=0; i<=len-1; i++) {
for(int j=0; j<=columnSize-1; i++) {
arr2D[i][j]=arr1D[k];
k++;
}
}
return arr2D;
}
public static String toHexString(byte[] hexByte) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < hexByte.length; i++)
str.append(String.format("%x", hexByte[i]));
return str.toString();
}
private static byte [] test() {
// Command String
String hex = "84";
//Creates the command byte array (Function #1)
int NumberChars = hex.length();
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2) {
int x = i+2;
bytes[i / 2] = new BigInteger(hex.substring(i, x),16).byteValue();
}
return bytes;
}