Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix convert support chinese #170

Merged
merged 1 commit into from Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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();
UDLD marked this conversation as resolved.
Show resolved Hide resolved
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("中文存证");
}



}