如何在 ColdFusion 中重写这个 .Net C#?
我不知道如何在 CF 中编写这样的 byteArray
b = new byte[] { 0x5e, 0x31, 0x9b, 0xdb, 60, 0x5b };
I have no idea how to write a byteArray like this in CF
b = new byte[] { 0x5e, 0x31, 0x9b, 0xdb, 60, 0x5b };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
ColdFusion 中的二进制对象是字节数组。这是一个示例:
此输出:
编辑
在回答这个问题时,我发现了 ColdFusion 的一些令人不快的地方...即,CF 显然将所有数字视为有符号,因此拒绝将超过 127 的值转换为字节字段。为了获得超过 127 的值(例如,在您的示例中为 0x9b/155),您必须将其转换为无符号。不幸的是,这并不容易做到。不过,我创建了一个小函数,使它变得更容易:
Binary objects in ColdFusion are Byte Arrays. Here is an example:
This outputs:
edit
I discovered something unpleasant about ColdFusion in answering this... namely, that CF apparently treats all numbers as signed, and so refuses to cast a value over 127 into a byte field. In order to get a value over 127 (such as, in your example, 0x9b/155), you have to cast it as unsigned. Unfortunately, this is not that easy to do. However, I've whipped up a little function which makes it easier:
我认为如果您想要一个真正的字节数组,您将需要使用 Java 来执行此操作,因为 ColdFusion 没有字节数组。
我想问你用它做什么,因为可能还有另一种解决方案,但是 这篇文章可能会帮助您创建 Java 字节数组
I think you will need to do this using Java if you want a true Byte array as ColdFusion doesn't have byte arrays.
I'd ask what your using it for as there is probably another solution, however, this post will probably assist you creating a Java Byte array
它们只是用十六进制表示大部分值。删除 CF 中未使用的前导
0x
。然后将所有内容连接成一个字符串,并使用 binaryDecode 将其转换为字节数组:编辑:如下所述,C# 代码在中途切换语法,并在十六进制中间放入一个无符号值value:
但是您不必在 CF 代码中执行相同的操作。事实上,如果您不这样做可能会更好,因为它需要更复杂的代码。通过仅使用十六进制,您可以在一行代码中创建字节数组。另外,它更加一致和可读。只需使用 formatBaseN 转换任何无符号值,例如
60
转为十六进制。They are just expressing most of the values in hexadecimal. Remove the leading
0x
's, which are not used in CF. Then concatenate everything into a single string and use binaryDecode to convert it to a byte array:Edit: As noted below the C# code switched syntax mid-stream and threw in one unsigned value in the middle of the hexadecimal values:
However you do not have to do the same in your CF code. In fact it is probably better if you do not, because it requires more complex code. By using only hex, you can create the byte array in a single line of code. Plus it is more consistent and readable. Just use formatBaseN to convert any unsigned values like
60
to hex.您还可以在 java 字符串上使用
.getBytes()
方法。例如:
http:// docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes%28%29
You can also use the
.getBytes()
method on java Strings.For example:
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes%28%29