Skip to content

Commit

Permalink
Merge pull request #170 from citahub/fix-convert-china-issue
Browse files Browse the repository at this point in the history
fix convert support chinese
  • Loading branch information
UDLD committed Apr 10, 2020
2 parents 078689a + 95808c2 commit bd5ce84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 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,40 @@ 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
23 changes: 19 additions & 4 deletions tests/src/test/java/com/citahub/cita/tests/StoreRecordTest.java
Expand Up @@ -35,12 +35,12 @@ public class StoreRecordTest {
cryptoTx = Transaction.CryptoTx.valueOf(conf.cryptoTx);
}

@Test
public void testStoreRecord()

public void testStoreRecord(String content)
throws IOException, InterruptedException {

String sampleDataToStore = ConvertStrByte
.stringToHexString("SampleDataToStore");
.stringToHexString(content);

CITAjSystemContract sysContract = new CITAjSystemContract(service);

Expand Down Expand Up @@ -74,6 +74,21 @@ public void testStoreRecord()

System.out.println(unverifiedTransaction);
String dataStored = unverifiedTransaction.getTransaction().getData().toStringUtf8();
assertThat(dataStored,equalTo("SampleDataToStore"));
assertThat(dataStored,equalTo(content));
}

@Test
public void testStoreEnglish() throws IOException, InterruptedException {

testStoreRecord("SampleDataToStore");
}

@Test
public void testStoreChinese() throws IOException, InterruptedException {

testStoreRecord("中文存证");
}



}

0 comments on commit bd5ce84

Please sign in to comment.