为什么80%的码农都做不了架构师?>>>
在C语言中通过memcpy很容易拷贝任一数据字节的数据(在数据允许的情况下),但是在JAVA可以用用什么方式替代呢?
在JAVA一般用二字节表示一个字母。
在做通讯交易的情况下,总会传很多报文,而报文总会时不时的包含一些结束符如'0x00',但是用常规的方式截取如 new String(buf, pos + 2, length); 来获得字符串,往往会出错(因为其遇到一些特殊的字符如结束符,会在计算字符串的操作出错)。但是用 new String(buf, pos + 2, length,"ASCII"),可以解决。
这里涉及到字符的字集问题(即最小单位)。
这里有一个测试的例子。关于8583报文的交易报文。
请求为:(前面增加了二个字节的长度)
----------------------------------------------------------------
01 05 60 00 03 00 00 61 31 00 31 10 02 01 00 70 24 06
C0 20 C0 9A 11 16 62 25 00 00 00 00 00 22 03 00
00 00 00 00 01 10 00 00 00 24 30 10 05 10 00 02
06 12 29 62 25 00 00 00 00 00 22 D3 01 02 01 00
00 0F 30 30 30 30 30 31 34 30 38 30 31 31 31 30
30 37 35 34 32 30 30 30 31 31 35 36 4A AA A5 BA
E2 BA EF F7 26 00 00 00 00 00 00 00 01 34 9F 26
08 2D C8 83 F9 35 57 DE A2 9F 27 01 80 9F 10 08
07 00 01 03 A0 00 02 01 9F 37 04 EE DD 84 5E 9F
36 02 00 1D 95 05 00 00 04 88 00 9A 03 14 02 28
9C 01 03 9F 02 06 00 00 00 01 10 00 5F 2A 02 01
56 82 02 7C 00 9F 1A 02 01 56 9F 03 06 00 00 00
00 00 00 9F 33 03 E0 E1 C8 9F 34 03 42 03 00 9F
35 01 22 9F 1E 08 30 32 31 35 30 33 37 30 9F 09
02 00 20 9F 41 04 00 00 00 24 84 08 A0 00 00 03
33 01 01 02 00 13 10 00 00 50 00 05 00 33 30 38
30 44 36 42 38
---------------------------------------------------------------------------
解析的内容为:
[F002] type=[ LLNVAR] len=[016] value=[6225000000000022]
[F003] type=[ NUMERIC] len=[006] value=[030000]
[F004] type=[ AMOUNT] len=[012] value=[000000011000]
[F011] type=[ NUMERIC] len=[006] value=[000024]
[F014] type=[ DATE4] len=[004] value=[0610]
[F022] type=[ NUMERIC] len=[003] value=[051]
[F023] type=[ NUMERIC] len=[003] value=[000]
[F025] type=[ NUMERIC] len=[002] value=[06]
[F026] type=[ NUMERIC] len=[002] value=[12]
[F035] type=[ LLNVAR] len=[029] value=[6225000000000022D301020100000]
[F041] type=[ ALPHA] len=[008] value=[00000140]
[F042] type=[ ALPHA] len=[015] value=[801110075420001]
[F049] type=[ ALPHA] len=[003] value=[156]
[F052] type=[ ALPHA] len=[011] value=[J衡猴? ]
[F053] type=[ NUMERIC] len=[016] value=[2600000000000000]
[F055] type=[ LLLVAR] len=[134] value=[?&-???5W???' ??
[F060] type=[ LLLNVAR] len=[013] value=[1000005000050]
[F064] type=[ ALPHA] len=[008] value=[3080D6B8]
可以看出F55域有很多特殊的字符存在。
==============================================
String c = new String(a.getBytes(), "ASCII");
System.out.println(c.length());