如何在 ColdFusion 中重写这个 .Net C#?

发布于 2025-01-01 09:00:24 字数 113 浏览 2 评论 0原文

我不知道如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漫漫岁月 2025-01-08 09:00:24

ColdFusion 中的二进制对象是字节数组。这是一个示例:

<cfset bytes = ToBinary(ToBase64("ABCDEF"))>

<cfloop array="#bytes#" index="byte">
<cfoutput>#byte#</cfoutput><br>
</cfloop>

此输出:

65
66
67
68
69
70

编辑

在回答这个问题时,我发现了 ColdFusion 的一些令人不快的地方...即,CF 显然将所有数字视为有符号,因此拒绝将超过 127 的值转换为字节字段。为了获得超过 127 的值(例如,在您的示例中为 0x9b/155),您必须将其转换为无符号。不幸的是,这并不容易做到。不过,我创建了一个小函数,使它变得更容易:

<cffunction name="uint">
    <cfargument name="val">
    <cfargument name="radix" default="16">
    <cfreturn CreateObject('java', 'org.apache.axis.types.UnsignedInt').init(InputBaseN(arguments.val, arguments.radix)).byteValue()>
</cffunction>

<cfset bin = JavaCast("byte[]", 
        [
            uint('5e'), 
            uint('31'), 
            uint('9b'), 
            uint('db'), 
            uint('db'), 
            uint('60', 10), 
            uint('5b')
        ])>
<cfdump var="#ToBase64(bin, 'utf-16')#">

Binary objects in ColdFusion are Byte Arrays. Here is an example:

<cfset bytes = ToBinary(ToBase64("ABCDEF"))>

<cfloop array="#bytes#" index="byte">
<cfoutput>#byte#</cfoutput><br>
</cfloop>

This outputs:

65
66
67
68
69
70

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:

<cffunction name="uint">
    <cfargument name="val">
    <cfargument name="radix" default="16">
    <cfreturn CreateObject('java', 'org.apache.axis.types.UnsignedInt').init(InputBaseN(arguments.val, arguments.radix)).byteValue()>
</cffunction>

<cfset bin = JavaCast("byte[]", 
        [
            uint('5e'), 
            uint('31'), 
            uint('9b'), 
            uint('db'), 
            uint('db'), 
            uint('60', 10), 
            uint('5b')
        ])>
<cfdump var="#ToBase64(bin, 'utf-16')#">
半衾梦 2025-01-08 09:00:24

我认为如果您想要一个真正的字节数组,您将需要使用 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

云胡 2025-01-08 09:00:24

它们只是用十六进制表示大部分值。删除 CF 中未使用的前导 0x。然后将所有内容连接成一个字符串,并使用 binaryDecode 将其转换为字节数组:

<!--- this is equivalent to 0x5e, 0x31, 0x9b, 0xdb, 60, 0x5b --->
<cfset bytes = binaryDecode("5e319bdb3c5b", "hex")>

编辑:如下所述,C# 代码在中途切换语法,并在十六进制中间放入一个无符号值value:

       0x5e - 94 in hex
       0x31 - 49 in hex
       0x9b - 155 in hex 
       0xdb - 219 in hex 
       60   - ** unsigned byte value
       0x5b - 91 in hex

但是您不必在 CF 代码中执行相同的操作。事实上,如果您不这样做可能会更好,因为它需要更复杂的代码。通过仅使用十六进制,您可以在一行代码中创建字节数组。另外,它更加一致和可读。只需使用 formatBaseN 转换任何无符号值,例如 60 转为十六进制。

       5e
       31 
       9b
       db
       3c   <= formatBaseN(60, 16)
       5b

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:

<!--- this is equivalent to 0x5e, 0x31, 0x9b, 0xdb, 60, 0x5b --->
<cfset bytes = binaryDecode("5e319bdb3c5b", "hex")>

Edit: As noted below the C# code switched syntax mid-stream and threw in one unsigned value in the middle of the hexadecimal values:

       0x5e - 94 in hex
       0x31 - 49 in hex
       0x9b - 155 in hex 
       0xdb - 219 in hex 
       60   - ** unsigned byte value
       0x5b - 91 in hex

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.

       5e
       31 
       9b
       db
       3c   <= formatBaseN(60, 16)
       5b
垂暮老矣 2025-01-08 09:00:24

您还可以在 java 字符串上使用 .getBytes() 方法。

例如:

<cfscript>
var test = "abc123";
var testBytes = test.getBytes("UTF-8");
</cfscript>

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:

<cfscript>
var test = "abc123";
var testBytes = test.getBytes("UTF-8");
</cfscript>

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes%28%29

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文