Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将字节数组转换为字符串时修剪填充值_C#_String - Fatal编程技术网

C# 将字节数组转换为字符串时修剪填充值

C# 将字节数组转换为字符串时修剪填充值,c#,string,C#,String,我在C#程序中使用的外部DLL给我一个指向内存中某个位置的指针。从那里,我将该位置的以下200个字节读入一个字节数组,然后将其转换为字符串 问题:在大多数情况下,我的代码可以工作,但它也读取DLL用作填充的值(似乎是205) 尝试:我尝试使用本文中的示例修剪字符串,但填充仍然存在。我的代码如下: const int BYTES_PER_DEVICE_NAME = 200; const char PADDING_VALUE = (char)205; const char NULL_VALUE =

我在C#程序中使用的外部DLL给我一个指向内存中某个位置的指针。从那里,我将该位置的以下200个字节读入一个字节数组,然后将其转换为字符串

问题:在大多数情况下,我的代码可以工作,但它也读取DLL用作填充的值(似乎是205)

尝试:我尝试使用本文中的示例修剪字符串,但填充仍然存在。我的代码如下:

const int BYTES_PER_DEVICE_NAME = 200;
const char PADDING_VALUE = (char)205;
const char NULL_VALUE = (char)0;

IntPtr nameAddress = (IntPtr)myDLL.GetPointer();

// Only run if the pointer is not to a null value
if (nameAddress != IntPtr.Zero)
{
    byte[] nameAsBytes = new byte[BYTES_PER_DEVICE_NAME];
    Marshal.Copy(nameAddress, nameAsBytes, 0, BYTES_PER_DEVICE_NAME);
    string nameAsString = System.Text.Encoding.UTF8.GetString(nameAsBytes).TrimEnd(new char[] { PADDING_VALUE, NULL_VALUE });

    // Add the string to deviceNames, a string array
    deviceNames[n] = nameAsString;
}
如下所示,即使我使用了
TrimEnd()
字符串
nameastring
仍然包含空值和填充值



我目前正在尝试查看是否可以在我的
nameAsBytes
字节数组中找到
\0
的索引,并且只解析该索引之前的字节。但是,我对
TrimEnd()
有什么错?

您可以使用UTF8Encoding.GetString方法(Byte[]), Int32, Int32)方法:

byte[]nameAsBytes=新字节[每个设备的字节名称];
Marshal.Copy(nameAddress、nameAsBytes、0、每个设备的字节数);
国际斯特伦;
for(strlen=0;strlen
您可以使用UTF8Encoding.GetString方法(字节[], Int32, Int32)方法:

byte[]nameAsBytes=新字节[每个设备的字节名称];
Marshal.Copy(nameAddress、nameAsBytes、0、每个设备的字节数);
国际斯特伦;
for(strlen=0;strlen
一旦字节数组转换为.NET字符串,它将采用UTF16编码,而char(205)将不会产生您想要的效果。任何大于127的值都可能在转换后映射到不同的代码点

在转换为字符串之前,请尝试删除205

const int BYTES_PER_DEVICE_NAME = 200;
const char PADDING_VALUE = (char)205;
const char NULL_VALUE = (char)0;

IntPtr nameAddress = (IntPtr)myDLL.GetPointer();

// Only run if the pointer is not to a null value
if (nameAddress != IntPtr.Zero)
{
    byte[] nameAsBytes = new byte[BYTES_PER_DEVICE_NAME];
    Marshal.Copy(nameAddress, nameAsBytes, 0, BYTES_PER_DEVICE_NAME);
    byte[] cleanedBytes = nameAsBytes.Where(a => a != PADDING_VALUE); //Filter out the bad bytes
    string nameAsString = System.Text.Encoding.UTF8.GetString(cleanedBytes);

    // Add the string to deviceNames, a string array
    deviceNames[n] = nameAsString;
}

一旦字节数组被转换成.NET字符串,它将采用UTF16编码,而char(205)将不会产生您想要的效果。任何大于127的值都可能在转换后映射到不同的代码点

在转换为字符串之前,请尝试删除205

const int BYTES_PER_DEVICE_NAME = 200;
const char PADDING_VALUE = (char)205;
const char NULL_VALUE = (char)0;

IntPtr nameAddress = (IntPtr)myDLL.GetPointer();

// Only run if the pointer is not to a null value
if (nameAddress != IntPtr.Zero)
{
    byte[] nameAsBytes = new byte[BYTES_PER_DEVICE_NAME];
    Marshal.Copy(nameAddress, nameAsBytes, 0, BYTES_PER_DEVICE_NAME);
    byte[] cleanedBytes = nameAsBytes.Where(a => a != PADDING_VALUE); //Filter out the bad bytes
    string nameAsString = System.Text.Encoding.UTF8.GetString(cleanedBytes);

    // Add the string to deviceNames, a string array
    deviceNames[n] = nameAsString;
}