如何从IronPython中检索c#字节数组(Byte [])?

时间:2022-01-15 07:20:02

I have a c# function that I can call from IronPython. The function returns a byte array that I'd like to convert to a string for display and compare.

我有一个可以从IronPython调用的c#函数。该函数返回一个字节数组,我想将其转换为字符串以进行显示和比较。

Python is telling me to pass the input parameter - (out Byte[] DataOut), below - as type "StrongBox[Array[Byte]]", so I converted "var" with

Python告诉我传递输入参数 - (out Byte [] DataOut),下面 - 作为类型“StrongBox [Array [Byte]]”,所以我转换为“var”

clr.Reference[Array[Byte]]() .

How do I convert this to a string?

如何将其转换为字符串?

namespace My_Library.My_Namespace
{
    /// </summary>
    public class My_App : OSI_Layer
    {

        public bool My_Function(out Byte[] DataOut)
        {

        // fill up DataOut with a string 

            return (Send(out DataOut));
        }

    // etc...
    }

}

//////////////////////////
//
// IronPython
//
// From IronPython I...

>>>
>>> import clr
>>> clr.AddReferenceToFileAndPath('My_Library.dll')
>>> from My_Library.My_Namespace import My_App
>>> App = My_App()
>>>
>>> from System import Array, Byte      
>>> var = clr.Reference[Array[Byte]]() # Create type StrongBox[Array[Byte]]"
>>>
>>> clr.Reference[Array[Byte]]
<type 'StrongBox[Array[Byte]]'>
>>>
>>> App.My_Function(var)
>>>
True
>>> var
<System.Byte[] object at 0x000000000000002B [System.Byte[]]>
>>>
>>> printable_var = System.BitConverter.ToString(var)

Traceback (most recent call last): File "", line 1, in TypeError: expected Array[Byte], got StrongBox[Array[Byte]]

回溯(最近一次调用最后一次):TypeError中的文件“”,第1行:expect Array [Byte],得到StrongBox [Array [Byte]]

1 个解决方案

#1


0  

You need to pass in the Value of the box, not the box itself.

您需要传入框的值,而不是框本身。

printable_var = System.BitConverter.ToString(var.Value)

#1


0  

You need to pass in the Value of the box, not the box itself.

您需要传入框的值,而不是框本身。

printable_var = System.BitConverter.ToString(var.Value)