LibreOffice Basic 是否可以使用无符号长整型?
Are unsigned long types available to LibreOffice Basic?
我想编写一个简单的 32 位 FNV hash function for LibreOffice Calc. However, LibreOffice Basic only supports signed long data types,因此您将在第 7 行使用以下代码得到 "Inadmissible value or data type. Overflow." 错误:
Function Hash(strText as String) as Long
Dim h As Long
Dim nextChar As String
Dim temp As Long
h = 2166136261
For i = 1 To Len(strText)
nextChar = Mid(strText, i, 1)
temp = Asc(nextChar)
h = h XOR temp
h = h * 16777619
Next
Hash = h
End Function
因为上面代码中h
变量赋值为2166136261,显然是越界了。是否可以在 LibreOffice Basic 中使用 unsigned long(0 到 4294967295)数据类型?如果是,怎么做?
你可以这样做:
Sub CallHash
oMasterScriptProviderFactory = createUnoService(_
"com.sun.star.script.provider.MasterScriptProviderFactory")
oScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScript = oScriptProvider.getScript(_
"vnd.sun.star.script:foo.py$hash?language=Python&location=user")
hashString = oScript.invoke(Array("bar"), Array(), Array())
MsgBox hashString
End Sub
foo.py:
def hash(strText):
h = 2166136261
for nextChar in strText:
temp = ord(nextChar)
h = h ^ temp
h = h * 16777619
return str(h)
或者放弃 Basic 并仅使用 Python-UNO。
UNO API 中存在无符号长整型值。但是,我没有找到任何 API 方法来对此对象执行计算。
Dim o As Object
o = CreateUnoValue("unsigned long", 2166136261)
我想编写一个简单的 32 位 FNV hash function for LibreOffice Calc. However, LibreOffice Basic only supports signed long data types,因此您将在第 7 行使用以下代码得到 "Inadmissible value or data type. Overflow." 错误:
Function Hash(strText as String) as Long
Dim h As Long
Dim nextChar As String
Dim temp As Long
h = 2166136261
For i = 1 To Len(strText)
nextChar = Mid(strText, i, 1)
temp = Asc(nextChar)
h = h XOR temp
h = h * 16777619
Next
Hash = h
End Function
因为上面代码中h
变量赋值为2166136261,显然是越界了。是否可以在 LibreOffice Basic 中使用 unsigned long(0 到 4294967295)数据类型?如果是,怎么做?
你可以这样做:
Sub CallHash
oMasterScriptProviderFactory = createUnoService(_
"com.sun.star.script.provider.MasterScriptProviderFactory")
oScriptProvider = oMasterScriptProviderFactory.createScriptProvider("")
oScript = oScriptProvider.getScript(_
"vnd.sun.star.script:foo.py$hash?language=Python&location=user")
hashString = oScript.invoke(Array("bar"), Array(), Array())
MsgBox hashString
End Sub
foo.py:
def hash(strText):
h = 2166136261
for nextChar in strText:
temp = ord(nextChar)
h = h ^ temp
h = h * 16777619
return str(h)
或者放弃 Basic 并仅使用 Python-UNO。
UNO API 中存在无符号长整型值。但是,我没有找到任何 API 方法来对此对象执行计算。
Dim o As Object
o = CreateUnoValue("unsigned long", 2166136261)