# How to use StringTools?

Dim File, Data, Text, StringTools, Strings
Set File = DOpus.FSUtil.OpenFile("d:\test\Clipboard Text.txt")
Set Text = DOpus.Create.Blob()
Set StringTools = DOpus.Create.StringTools

Dopus.Output File.Size

File.Seek 0, "b"
File.Close
Strings = StringTools.Encode(Text, "utf-8")
Strings = StringTools.Decode(Text)

Dopus.outPut Strings

6024 Error at line 11, position 2 无效的过程调用或参数: 'Encode' (0x800a0005)

A Blob can currently only be encoded as "base64" or "quoted". If you want to encode as UTF-8 you have to pass the data as a string.

@jon

Dim File, Data, Text, Text1, StringTools, Strings
Set File = DOpus.FSUtil.OpenFile("d:\test\Clipboard Text.txt")
Set Text = DOpus.Create.Blob()
Set Text1 = DOpus.Create.Blob()
Set StringTools = DOpus.Create.StringTools

Dopus.Output File.Size

File.Seek 0, "b"
File.Close
Text1 = StringTools.Encode(Text, "base64")
Strings = StringTools.Decode(text1, "base64")

Error at line 14, position 2
A method was called unexpectedly (0x8000ffff)

Decoding base64 gets you binary data back. The result is not necessarily a printable string. (For example, it could be a JPEG file.)

Decode(x, "base64") always returns a Blob object, essentially an array of bytes.

What is the overall thing that you are trying to do?

StringTools Encode and Decode cannot get the correct value

Code

Function PadLeft(ByVal PadStr, ByVal PadChar, ByVal PadLen)
End Function

End Function

Function IIf(ByVal Expression, ByVal TruePart, ByVal FalsePart)
If Expression Then IIf = TruePart Else IIf = FalsePart
End Function

Function HHex(ByVal Value)
If Len(Hex(Value)) Mod 2 Then HHex = "0" & Hex(Value) Else HHex = Hex(Value)
End Function

Sub OutputBlob(ByVal BlobValue, ByVal Interval) 'Interval = 1, 2, 4, 8, 16
Dim i, HexStr
If Interval = 1 Or Interval = 2 Or Interval = 4 Or Interval = 8 Or Interval = 16 Then Interval = Interval Else Interval = 1
for i = 0 to BlobValue.size - 1
HexStr = HexStr & IIf(i = 0, PadRight(Hex(i), 0, 8), IIf(i mod 16, "", vbCRLF & PadRight(Hex(i), 0, 8)))  &  IIf(i=0, " ", IIF(i mod Interval, "", " ")) & HHex(BlobValue(i))
next
Dopus.Output Trim(HexStr)
End Sub

const text = "test用必要的信息形成字符串。"
dim bo,st,arr
set st = dopus.create.stringtools
set bo = st.encode(text,"utf-8") 'unicodeFFFE, unicode, koi8-ru, koi8-r, big5, urf-7, utf-8, iso-8859-1 - iso-8859-9, euc-jp, euc-kr, gb2312
arr = bo.toarray
dopus.output String(60, "=")
OutputBlob bo, 1
dopus.output "String: " & st.decode(bo,"utf-8")
dopus.output String(60, "=")

set bo = st.encode(text,"unicodeFFFE")
OutputBlob bo, 1
dopus.output String(60, "=")

set bo = st.encode(text,"unicode")
OutputBlob bo, 1
dopus.output String(60, "=")

DOpus.FSUtil.OpenFile("E:\test\test.txt").Read bo ' test.txt Content = const text
OutputBlob bo, 1
dopus.output "String: " & st.decode(bo,"unicodeFFFE")



Return

============================================================
00000000 74 65 73 74 E7 94 A8 E5 BF 85 E8 A6 81 E7 9A 84
00000010 E4 BF A1 E6 81 AF E5 BD A2 E6 88 90 E5 AD 97 E7
00000020 AC A6 E4 B8 B2 E3 80 82 00
String: test用必要的信息形成字符串。
============================================================
00000000 00
============================================================
00000000 00
============================================================
00000000 FF FE 63 00 6F 00 6E 00 73 00 74 00 20 00 74 00
00000010 65 00 78 00 74 00 20 00 3D 00 20 00 22 00 74 00
00000020 65 00 73 00 74 00 28 75 C5 5F 81 89 84 76 E1 4F
00000030 6F 60 62 5F 10 62 57 5B 26 7B 32 4E 02 30 22 00
00000040 0D 00 0A 00
String:


The Help file is written in

Otherwise, format must be set to a valid code-page name (e.g. "utf-8", "gb2312" etc.), or a Windows code-page ID (e.g. 936, 65001). The source will be encoded using the specified code-page and a Blob is returned.

However, the Windows Code-page ID is not actually available, for example:
set bo = st.encode(text,65001)

Error
无效的过程调用或参数: 'encode' (0x800a0005)

Stringtools cannot encode the decoding of Unicode problems in the version of 12.5 no revision

This thread is not very clear. It also contains more than one question, which makes things harder to understand when the questions are unclear.

We can try to help but the language barrier makes things difficult, and time consuming.

If you think there is an issue with StringTools and certain encodings, please provide a short, complete script example that shows the problem.

The large script above has a lot of things that don't look relevant to the problem. The problem is not clearly explained in the thread. This combination makes it difficult for us to know exactly what we should look at.

The smaller pieces of one or two line script code are not complete enough for us to understand the whole problem.

We need a script that we can run to see the problem, but that does not have any extra things that are not related to the problem. Then we should be able to understand what is going wrong in Opus, or what is being misunderstood about how to use the scripting components.

Try providing the numeric code page as a string (e.g. "65001") and see if it makes a difference.

"unicodeFFFE" also has no return value

const text = "汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字字"

set st = dopus.create.stringtools
set bo = st.encode(text,"unicodeFFFE")
dopus.output bo.size
dopus.output st.Decode(bo,"unicodeFFFE")


eng:
Stringtools cannot encode the decoding of Unicode strings when can the problem be solved?
chs:
StringTools 无法编码解码unicode字符串的问题什么时候能够解决？

Windows does not support "unicodeFFFE" as a code page for desktop applications:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd317756(v=vs.85).aspx

Similar code works if using a supported codepage:

Help me understand the stringtools script. I'm confused!!!
You need:

1. Create a text file with the encoding "UTF-8".
2. Write the text to the file. For example: "Welcome to UTF-8 encoded text files!!!".
var filepath = "D:\\test\\text_utf8.txt"
var textFile = DOpus.FSUtil.OpenFile(filepath,"wa")
DOpus.Create.StringTools.Encode(filepath,"utf-8") // Don't work!!!


The 3rd line doesn't work!

var filepath = "D:\\test\\text_utf8.txt";
var filecontent = "Welcome to UTF-8 encoded text files!";
var encoding = "utf-8"; // Use "utf-8 bom" if you want a BOM

var ST = DOpus.Create.StringTools;
var FSU = DOpus.FSUtil;

var utf8blob = ST.Encode(filecontent, encoding);

var file = FSU.OpenFile(filepath, "wa");

if (file.error == 0)
{
file.Write(utf8blob);
file.Close();
}

1 Like