Skip to content

ASC


返回字符对应的 ASCII 或 Unicode 整数值。

语法

vb
declare function Asc ( byref str as const string, byval position as integer = 1 ) as ulong
declare function Asc ( byval str as const zstring ptr, byval position as integer = 1 ) as ulong
declare function Asc ( byval str as const wstring ptr, byval position as integer = 1 ) as ulong

用法

result = Asc( str [, position ] )

参数

str

源字符串。

position

字符在字符串中的位置。

返回值

返回存储在 strposition 位置的原始字符值。

strposition 都可以在编译时求值(如 Asc("a")Asc(chr(97))Asc("abc", 2) 等),则返回 uinteger 类型的结果,否则返回 ulong 类型的结果。

说明

strStringZString,返回该 position 处的 UByte 值。这将是 7 位 ASCII 码,或根据 str 中存储的字符串数据,也可能是某个代码页中的 8 位字符值。

strWString,返回该 position 处的 UShort(Windows)或 ULong(Linux)值。Windows 上为 16 位值(WString 使用 UTF-16),Linux 上为 32 位值(WString 使用 UTF-32)。

若字符串长度为零、position 小于一(1),或 position 大于 str 中的字符数,则函数返回零(0)。

Chr 执行与之相反的操作(针对 ASCII 字符串),WChr 则对 Unicode 字符串执行反操作,返回由传入的代码所表示字符组成的字符串。

示例

vb
Print "the ascii code of 'a' is:"; Asc("a")
Print "the ascii code of 'b' is:"; Asc("abc", 2)

输出结果为:

the ascii code of 'a' is: 97
the ascii code of 'b' is: 98

Unicode 示例:

vb
dim a as wstring * 12
a = "Привет, мир"
print "the Unicode of the second char of " & a & " is: " & asc(a, 2)

输出结果为:

the Unicode of the second char of Привет, мир is: 1088

平台差异

  • DOS 不支持 ASC 的宽字符字符串版本。

与 QB 的差异

  • 可选的 position 参数是 FreeBASIC 新增的。
  • QB 不支持 ASC 的宽字符字符串版本。

另请参阅

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系