将十六进制值存储在扩展名为 .ini 的记事本文件中 如何仅通过 CAPL 以十六进制读取它
stored hex values in notepad file with .ini extension how to read it in hex only via CAPL
我已将十六进制值存储在一个文本文件中,扩展名为 .ini 以及地址。但是当我阅读它时,它不会是十六进制格式,而是字符格式,所以有什么方法可以读取十六进制值并将其存储在 C 语言或 CAPL 脚本中的字节中?
我假设您知道如何在 CAPL 中读取文本文件...
您可以使用 strtol(char s[], long result&):long
将十六进制字符串转换为数字。查看 CAPL 帮助(CAPL 函数概述 -> 常规 -> strol):
The number base is
- haxadecimal if the string starts with "0x"
- octal if the string starts with "0"
- decimal otherwise
Whitespace (space or tabs) at the start of the staring are ignored.
示例:
on start
{
long number1, number2;
strtol("0xFF", number1);
strtol("-128", number2);
write("number1 = %d", number1);
write("number2 = %d", number2);
}
输出:
number1 = 255
number2 = -128
另请参阅:strtoll()
、strtoul()
、strtoull()
、strtod()
和 atol()
更新:
如果十六进制字符串不是以“0x”开头...
on message 0x200
{
if (this.byte(0) == hextol("38"))
write("byte(0) == 56");
}
long hextol(char s[])
{
long res;
char xs[8];
strncpy(xs, "0x", elcount(xs)); // cpy "0x" to 'xs'
strncat(xs, s, elcount(xs)); // cat 'xs' and 's'
strtol(xs, res); // convert to long
return res;
}
我已将十六进制值存储在一个文本文件中,扩展名为 .ini 以及地址。但是当我阅读它时,它不会是十六进制格式,而是字符格式,所以有什么方法可以读取十六进制值并将其存储在 C 语言或 CAPL 脚本中的字节中?
我假设您知道如何在 CAPL 中读取文本文件...
您可以使用 strtol(char s[], long result&):long
将十六进制字符串转换为数字。查看 CAPL 帮助(CAPL 函数概述 -> 常规 -> strol):
The number base is
- haxadecimal if the string starts with "0x"
- octal if the string starts with "0"
- decimal otherwise
Whitespace (space or tabs) at the start of the staring are ignored.
示例:
on start
{
long number1, number2;
strtol("0xFF", number1);
strtol("-128", number2);
write("number1 = %d", number1);
write("number2 = %d", number2);
}
输出:
number1 = 255
number2 = -128
另请参阅:strtoll()
、strtoul()
、strtoull()
、strtod()
和 atol()
更新:
如果十六进制字符串不是以“0x”开头...
on message 0x200
{
if (this.byte(0) == hextol("38"))
write("byte(0) == 56");
}
long hextol(char s[])
{
long res;
char xs[8];
strncpy(xs, "0x", elcount(xs)); // cpy "0x" to 'xs'
strncat(xs, s, elcount(xs)); // cat 'xs' and 's'
strtol(xs, res); // convert to long
return res;
}