Skip to content

Commit

Permalink
RFID 自动选择压缩方式
Browse files Browse the repository at this point in the history
  • Loading branch information
DigitalPlatform committed Dec 23, 2018
1 parent bd273f5 commit 2bdf281
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 11 deletions.
52 changes: 42 additions & 10 deletions DigitalPlatform.RFID/Compress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ namespace DigitalPlatform.RFID
{
public class Compress
{

public static string AutoSelectCompressMethod(string text)
{
if (CheckInteger(text, false))
return "integer";
if (CheckDigit(text, false))
return "digit";
if (CheckBit5(text, false))
return "bit5";
if (CheckBit6(text, false))
return "bit6";
if (CheckBit7(text, false))
return "bit7";
return "";
}

#region Integer

public const UInt64 MaxInteger = 9999999999999999999; // 19 位
Expand Down Expand Up @@ -108,7 +124,7 @@ static bool CheckInteger(string text, bool throwException = true)
if (ch < '0' || ch > '9')
{
if (throwException)
throw new ArgumentException("整型数压缩内容中不应包含非数字字符");
throw new ArgumentException($"用于整型数压缩的内容中出现了非数字字符 '{ch}'");
return false;
}
}
Expand Down Expand Up @@ -150,7 +166,6 @@ public static byte[] DigitCompress(string text)
i++;
}


return bytes.ToArray();
}

Expand All @@ -175,6 +190,16 @@ public static string DigitExtract(byte[] data)
// 检查字符串是否符合数字压缩的要求
static bool CheckDigit(string text, bool throwException = true)
{
foreach (char ch in text)
{
if (ch < '0' || ch > '9')
{
if (throwException)
throw new ArgumentException($"用于数字压缩的内容中出现了非法字符 '{ch}'");
return false;
}
}

if (text.Length < 2)
{
if (throwException)
Expand Down Expand Up @@ -270,12 +295,13 @@ static bool CheckBit5(string text, bool throwException = true)
return false;
}

// A-_ 之间是合法字符
foreach (char ch in text)
{
if (ch < 0x41 || ch > 0x5f)
{
if (throwException)
throw new ArgumentException("5 bit 压缩内容字符应该在 x41x5f 之间");
throw new ArgumentException($"用于 5 bit 压缩的内容字符出现了 0x410x5f 之外的字符 '{ch}'");
return false;
}
}
Expand Down Expand Up @@ -568,12 +594,13 @@ static bool CheckBit6(string text, bool throwException = true)
return false;
}

// 空-_ 之间为合法字符
foreach (char ch in text)
{
if (ch < 0x20 || ch > 0x5f)
{
if (throwException)
throw new ArgumentException("用于 6 bit 压缩的内容字符应该在 x20x5f 之间");
throw new ArgumentException($"用于 6 bit 压缩的内容字符出现了在 0x200x5f 之外的字符 '{ch}'");
return false;
}
}
Expand All @@ -591,7 +618,7 @@ static bool CheckBit6(string text, bool throwException = true)
if (count > 0)
{
if (throwException)
throw new ArgumentException("用于 6 bit 压缩的内容字符的尾部不应出现 x20 字符");
throw new ArgumentException("用于 6 bit 压缩的内容字符的尾部不应出现 0x20 字符");
return false;
}

Expand Down Expand Up @@ -1136,6 +1163,7 @@ static bool CheckIsil(string text, bool throwException = true)

public static string IsilExtract(byte[] data)
{
char save_charset = 'u';
char current_charset = 'u';
BitExtract extract = new BitExtract(data);
StringBuilder result = new StringBuilder();
Expand All @@ -1159,6 +1187,7 @@ public static string IsilExtract(byte[] data)
// 小写 shift
if (bits == "11101")
{
save_charset = current_charset;
current_charset = 'L';
continue;
}
Expand All @@ -1173,6 +1202,7 @@ public static string IsilExtract(byte[] data)
// 数字 shift
if (bits == "11111")
{
save_charset = current_charset;
current_charset = 'D';
continue;
}
Expand All @@ -1191,6 +1221,7 @@ public static string IsilExtract(byte[] data)
// 大写 shift
if (bits == "11101")
{
save_charset = current_charset;
current_charset = 'U';
continue;
}
Expand All @@ -1205,6 +1236,7 @@ public static string IsilExtract(byte[] data)
// 数字 shift
if (bits == "11111")
{
save_charset = current_charset;
current_charset = 'D';
continue;
}
Expand All @@ -1223,6 +1255,7 @@ public static string IsilExtract(byte[] data)
// 大写 shift
if (bits == "1101")
{
save_charset = current_charset;
current_charset = 'U';
continue;
}
Expand All @@ -1237,6 +1270,7 @@ public static string IsilExtract(byte[] data)
// 小写 shift
if (bits == "1111")
{
save_charset = current_charset;
current_charset = 'L';
continue;
}
Expand All @@ -1253,13 +1287,12 @@ public static string IsilExtract(byte[] data)
else if (ch >= 0x01 && ch <= 0x1a)
{
result.Append((char)(ch - 1 + 'A'));
continue;
}
else
throw new Exception("error 1");

if (current_charset == 'U')
current_charset = 'u';
current_charset = save_charset;
continue;
}

Expand All @@ -1272,13 +1305,12 @@ public static string IsilExtract(byte[] data)
else if (ch >= 0x01 && ch <= 0x1a)
{
result.Append((char)(ch - 1 + 'a'));
continue;
}
else
throw new Exception("error 2");

if (current_charset == 'L')
current_charset = 'l';
current_charset = save_charset;
continue;
}

Expand All @@ -1297,7 +1329,7 @@ public static string IsilExtract(byte[] data)
throw new Exception("error 3");

if (current_charset == 'D')
current_charset = 'd';
current_charset = save_charset;
continue;
}

Expand Down
66 changes: 65 additions & 1 deletion TestDp2Library/TestRfid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,36 @@ public class TestRfid
#region integer

[TestMethod]
public void Test_rfid_1()
public void Test_integer_1()
{
TestInteger("999999",
MakeBytes(999999));

// ‭DE0B6B3A763FFFF‬

}

[TestMethod]
public void Test_integer_2()
{
TestInteger("10", new byte[] { (byte)10 });
}

[TestMethod]
public void Test_integer_3()
{
// GB/T 35660.2-2017 page 31 例子
TestInteger("123456789012",
new byte[] { 0x1c, 0xbe, 0x99, 0x1a, 0x14 });
}

[TestMethod]
public void Test_integer_4()
{
// GB/T 35660.2-2017 page 32 例子
TestInteger("1203", new byte[] { 0x04, 0xb3 });
}

// 测试一些较小的数值
[TestMethod]
public void Test_loop_integer_1()
Expand Down Expand Up @@ -165,6 +185,15 @@ public void Test_bit6_2()
});
}

[TestMethod]
public void Test_bit6_3()
{
// GB/T 35660.2-2017 page 32 例子
TestBit6("QA268.L55", new byte[] {
0x44, 0x1c, 0xb6, 0xe2, 0xe3, 0x35, 0xd6
});
}

void TestBit6(string text, byte[] correct)
{
byte[] result = Compress.Bit6Compress(text);
Expand Down Expand Up @@ -293,6 +322,12 @@ public void Test_isil_1()
[TestMethod]
public void Test_isil_2()
{
//StringBuilder debugInfo = new StringBuilder();
//Compress.IsilCompress("US-InU-Mu", debugInfo);

TestIsil("US-InU-Mu", new byte[] {
0xac,0xc0,0x9e,0xba,0xa0,0x6f,0x6b
});
}

void TestIsil(string text, byte[] correct)
Expand All @@ -304,6 +339,35 @@ void TestIsil(string text, byte[] correct)
Assert.AreEqual(text, Compress.IsilExtract(result));
}

#endregion

#region AutoSelectCompressMethod()

[TestMethod]
public void Test_autoSelect_1()
{
// page 31 例子
Assert.AreEqual(
"integer",
Compress.AutoSelectCompressMethod("123456789012"));
}

[TestMethod]
public void Test_autoSelect_2()
{
// page 32 例子
Assert.AreEqual(
"integer",
Compress.AutoSelectCompressMethod("1203"));
}

[TestMethod]
public void Test_autoSelect_5()
{
Assert.AreEqual(
"bit6",
Compress.AutoSelectCompressMethod("QA268.L55"));
}

#endregion
}
Expand Down

0 comments on commit 2bdf281

Please sign in to comment.