Skip to content

Commit

Permalink
fix convert issue
Browse files Browse the repository at this point in the history
  • Loading branch information
UDLD committed Apr 7, 2020
1 parent 078689a commit efe16e5
Showing 1 changed file with 25 additions and 20 deletions.
@@ -1,8 +1,5 @@
package com.citahub.cita.protobuf;

import java.util.Collections;
import java.util.List;

public class ConvertStrByte {

public static String bytesToHexString(byte[] b) {
Expand Down Expand Up @@ -31,7 +28,7 @@ public static byte[] hexStringToBytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = (byte) Integer
ret[i] = Integer
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
Expand All @@ -40,7 +37,7 @@ public static byte[] hexStringToBytes(String src) {
/**
* Convert hex string to byte array with fixed bit length
*
* @param src hex string without "0x"
* @param src hex string without "0x"
* @param length length of bits of data to be sent to node.
* @return byte array converted from src.
*/
Expand All @@ -53,7 +50,7 @@ public static byte[] hexStringToBytes(String src, int length) {
int l = src.length() / 2;
byte[] value = new byte[l];
for (int i = 0; i < l; i++) {
value[i] = (byte) Integer
value[i] = Integer
.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}

Expand All @@ -69,33 +66,41 @@ public static byte[] hexStringToBytes(String src, int length) {


public static String stringToHexString(String strPart) {
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < strPart.length(); i++) {
int ch = (int) strPart.charAt(i);
String strHex = Integer.toHexString(ch);
hexString.append(strHex);
char[] chars = "0123456789abcdef".toCharArray();
StringBuilder sb = new StringBuilder();
byte[] bs = strPart.getBytes();
int bit;
for (int i = 0;
i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
return hexString.toString();
return sb.toString().trim();
}


public static String hexStringToString(String src) {
String temp = "";
for (int i = 0; i < src.length() / 2; i++) {
temp = temp
+ (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),
16).byteValue();
String str = "0123456789abcdef";
char[] hexs = src.toCharArray();
byte[] bytes = new byte[src.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return temp;
return new String(bytes);
}


public static Byte charToByte(Character src) {
return Integer.valueOf((int)src).byteValue();
return Integer.valueOf((int) src).byteValue();
}


private static String intToHexString(int a,int len) {
private static String intToHexString(int a, int len) {
len <<= 1;
String hexString = Integer.toHexString(a);
int b = len - hexString.length();
Expand Down

0 comments on commit efe16e5

Please sign in to comment.