回答編集履歴
1
追記
test
CHANGED
@@ -2,17 +2,55 @@
|
|
2
2
|
この関数内で、抽出した部分をbyte列に変換し、それを'utf-16be'として解釈して文字列化します。
|
3
3
|
なお、以下コードにて文字列に[:-1]しているのは、今回の問題では本質ではないので説明は省きます。
|
4
4
|
([Python raw strings and trailing backslash [duplicate]](https://stackoverflow.com/questions/2870730/python-raw-strings-and-trailing-backslash)に記載されています)
|
5
|
+
## 追記
|
6
|
+
`X2`にくわえ顔文字など`X4`にも対応させました。
|
7
|
+
参考:
|
8
|
+
[How to decoding IFC using Ruby](https://stackoverflow.com/questions/43417411/how-to-decoding-ifc-using-ruby)
|
9
|
+
[STEP-file, ISO 10303-21](https://www.loc.gov/preservation/digital/formats/fdd/fdd000448.shtml)
|
10
|
+
[6.4.3.3 Encoding ISO 10646 characters within a string](https://www.steptools.com/stds/step/IS_final_p21e3.html)
|
11
|
+
|
5
12
|
```Python
|
6
13
|
import re
|
7
14
|
|
8
|
-
def
|
15
|
+
def func2(x):
|
9
16
|
b = bytes.fromhex(x.group(1))
|
10
17
|
return b.decode('utf-16be')
|
11
18
|
|
19
|
+
def func4(x):
|
20
|
+
# 7文字毎の先頭に"0"を付加
|
21
|
+
#
|
22
|
+
# https://www.steptools.com/stds/step/IS_final_p21e3.html
|
12
|
-
ES
|
23
|
+
# 6.4.3.3 Encoding ISO 10646 characters within a string
|
24
|
+
#-----
|
25
|
+
# NOTE This use of eight hexadecimal characters in the "\X4\" encoding predates the restriction of the UCS codespace to a maximum value of 10FFFF.
|
26
|
+
# The first two characters in each eight character group will always be digit zero.
|
27
|
+
#-----
|
28
|
+
s = x.group(1)
|
29
|
+
s = ''.join(['0'+s[i:i+7] for i in range(0,len(s),7)])
|
13
30
|
|
31
|
+
b = bytes.fromhex(s)
|
32
|
+
return b.decode('utf-32be')
|
33
|
+
|
34
|
+
def decode_ifc_str(s):
|
35
|
+
EXPR2 = r'\\X2\\(.*?)\\X0\\='[:-1]
|
36
|
+
EXPR4 = r'\\X4\\(.*?)\\X0\\='[:-1]
|
37
|
+
for expr, func in [(EXPR2, func2), (EXPR4, func4)]:
|
38
|
+
s = re.sub(expr, func, s)
|
39
|
+
return s
|
40
|
+
|
41
|
+
lst = [
|
42
|
+
r'abc=',
|
14
|
-
|
43
|
+
r'\X2\3010914D7BA1301151B75A92914D7BA1\X0\ (b)15.9\X2\03C600D7\X0\9.5\X2\03C6\X0\=',
|
44
|
+
r'\X2\03B103B203B3\X0\=', # ギリシャ文字アルファ、ベータ、ガンマ(αβγ)
|
45
|
+
r'\X4\001F600\X0\=', # ニヤリと笑う顔(絵文字、😀)
|
46
|
+
r'\X4\001F600001F638\X0\=' #ニヤリと笑う猫の顔(2つの絵文字、😀😸)
|
47
|
+
]
|
48
|
+
|
49
|
+
with open('ret.txt', 'w', encoding='utf-8') as f:
|
50
|
+
for src in lst:
|
15
|
-
dst =
|
51
|
+
dst = decode_ifc_str(src)
|
52
|
+
line = f'[{src}]->[{dst}]'
|
16
|
-
print(
|
53
|
+
print(line) # 環境によっては顔文字が正しく表示できない
|
54
|
+
f.write(line+'\n')
|
17
55
|
```
|
18
56
|
|