Visual FoxPro (VFP) CTOBIN 和 BINTOC 函数 - .Net 中的等效函数
我们正在重写一些以前在 Visual FoxPro 中开发的应用程序,并使用 .Net(使用 C#)重新开发它们,
这是我们的场景: 我们的应用程序使用智能卡。我们从具有名称和号码的智能卡中读取数据。名称以可读文本形式正常返回,但数字(在本例中为“900”)以 2 字节字符表示形式返回(131 和 132),如下所示 - ▪
这 2 个特殊字符可以在扩展 Ascii 中看到表..现在你可以看到2个字节是131和132,并且可能会有所不同,因为没有单一的标准扩展ascii表(据我所知,阅读这里的一些帖子)
所以...智能卡之前是使用 VFP 中的 BINTOC 函数写入的,因此 900 被写入卡中为 ▪。在 FoxPro 中,这 2 个特殊字符可以使用 CTOBIN 函数转换回整数格式.. FoxPro 中的另一个内置函数..
所以(终于进入正题) - 到目前为止,我们无法将这 2 个特殊字符转换回整数格式一个 int ( 900 ),我们想知道在 .NET 中是否可以将整数的字符表示读回实际的整数。
或者有没有办法用C#重写这2个VFP函数的逻辑?
更新: 经过一番摆弄后,我们意识到要将 900 转换为 2 个字节,我们需要将 900 转换为 16 位二进制值,然后我们需要将该 16 位二进制值转换为十进制值。
因此,如上所述,我们收到返回 131 和 132 及其相应的二进制值,即 10000011 (十进制值 131 )和 10000100 (十进制值 132 )。 当我们将这两个值连接到“1000001110000100”时,它会给出十进制值 33668,但是如果我们删除前导 1 并将“000001110000100”转换为十进制,它会给出正确的值 900...
不太确定为什么会这样...
任何帮助将不胜感激。
We are rewriting some applications previously developed in Visual FoxPro and redeveloping them using .Net ( using C# )
Here is our scenario:
Our application uses smartcards. We read in data from a smartcard which has a name and number. The name comes back ok in readable text but the number, in this case '900' comes back as a 2 byte character representation (131 & 132) and look like this - ƒ„
Those 2 special characters can be seen in the extended Ascii table.. now as you can see the 2 bytes are 131 and 132 and can vary as there is no single standard extended ascii table ( as far as I can tell reading some of the posts on here )
So... the smart card was previously written to using the BINTOC function in VFP and therefore the 900 was written to the card as ƒ„. And within foxpro those 2 special characters can be converted back into integer format using CTOBIN function.. another built in function in FoxPro..
So ( finally getting to the point ) - So far we have been unable to convert those 2 special characters back to an int ( 900 ) and we are wondering if this is possible in .NET to read the character representation of an integer back to an actual integer.
Or is there a way to rewrite the logic of those 2 VFP functions in C#?
UPDATE:
After some fiddling we realise that to get 900 into 2bytes we need to convert 900 into a 16bit Binary Value, then we need to convert that 16 bit binary value into a decimal value.
So as above we are receiving back 131 and 132 and their corresponding binary values as being 10000011 ( decimal value 131 ) and 10000100 ( decimal value 132 ).
When we concatenate these 2 values to '1000001110000100' it gives the decimal value 33668 however if we removed the leading 1 and transform '000001110000100' to decimal it gives the correct value of 900...
Not too sure why this is though...
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 VFP 将您的值存储为带符号的 16 位(短)整数。对我来说,负数似乎有一个奇怪的转换点,但它将 128 添加到 8 位数字,并将 32768 添加到 16 位数字。
因此,从字符串转换 16 位数字应该像将其读取为 16 位整数,然后从中取出 32768 一样简单。如果您必须手动执行此操作,则必须将第一个数字乘以 256,然后添加第二个数字以获得存储的值。然后从该数字中减去 32768 即可得到您的值。
示例:
您可以尝试按照 http://msdn.microsoft 使用 C# 转换。 com/en-us/library/ms131059.aspx 和 http://msdn.microsoft.com/en-us/library/tw38dw27.aspx 至少为您完成一些工作,但如果没有,应该不会太难手动编写上述代码。
It looks like VFP is storing your value as a signed 16 bit (short) integer. It seems to have a strange changeover point to me for the negative numbers but it adds 128 to 8 bit numbers and adds 32768 to 16 bit numbers.
So converting your 16 bit numbers from the string should be as easy as reading it as a 16 bit integer and then taking 32768 away from it. If you have to do this manually then the first number has to be multiplied by 256 and then add the second number to get the stored value. Then take 32768 away from this number to get your value.
Examples:
You could try using the C# conversions as per http://msdn.microsoft.com/en-us/library/ms131059.aspx and http://msdn.microsoft.com/en-us/library/tw38dw27.aspx to do at least some of the work for you but if not it shouldn't be too hard to code the above manually.
虽然晚了几年,但这里有一个有效的例子。
这是 CTOBIN 的 VFP 8 及更早版本的等效项,它涵盖了您的场景。您应该能够根据上面的代码编写自己的 BINTOC。 VFP 9 添加了对多个选项的支持,例如非反转二进制数据、货币和双精度数据类型以及有符号值。此示例仅涵盖反向无符号二进制文件,如支持的旧版 VFP。
一些注意事项:
无符号数值最高可达
System.UInt64
。结果降至您预期的数字类型,您应该验证
天花板。例如,如果您需要 Int32,则检查结果
在执行转换之前针对
Int32.MaxValue
。字节数组。您需要了解使用哪种编码
读取字符串,然后应用相同的编码来获取字节数组
在调用该函数之前。在 VFP 世界中,这种情况经常发生
Encoding.ASCII
,但这取决于应用程序。It's a few years late, but here's a working example.
This is a VFP 8 and earlier equivalent for CTOBIN, which covers your scenario. You should be able to write your own BINTOC based on the code above. VFP 9 added support for multiple options like non-reversed binary data, currency and double data types, and signed values. This sample only covers reversed unsigned binary like older VFP supported.
Some notes:
unsigned numeric values up to
System.UInt64
.result down to your expected numeric type, you should verify the
ceiling. For example, if you need an Int32, then check the result
against
Int32.MaxValue
before you perform the cast.byte array. You would need to understand which encoding was used to
read the string, then apply that same encoding to get the byte array
before calling this function. In the VFP world, this is frequently
Encoding.ASCII
, but it depends on the application.