I can read the complete CNC program of a remote machine to a byte buffer with a third party dll. After that I have to read the byte buffer line by line and transfer it to a string array.(I don't want to write the program to a file, I want to show the program lines line by line in my Blazor page)

I have written following code, where I read the complete buffer into one string. But I cannot split it to lines. How can I do that? I should normally read the CNC program for example like:

G0 G603

AAA:

Z=IC(-50)

Z=IC(50)

GOTOB AAA

M30

But my code is reading the CNC program as one line.

G0 G603 AAA: Z=IC(-50) Z=IC(50) GOTOB AAA M30

public Int32 doNCK_CopyFromNCAlloc(Int32 connnr, Int32 timeout, string path, out string  CNC_File_Out)
    {
        Int32 result = 0;
        Byte[] buff = null;
        Int32 bufflen = 0;
        String filename = path;
        CNC_File_Out = "";
        result = AGL4.NCK_CopyFromNCAlloc(connnr, filename, out buff, out bufflen, timeout);

     if (result == 0)
        {                
            b = buff;             
            parameter_value_string = BitConverter.ToString(b, 0);
            parameter_value_string_UTF8 = System.Text.Encoding.UTF8.GetString(b);
            CNC_File_Out= parameter_value_string_UTF8.Replace("\0", string.Empty);
        }            
        Return_Read_CNC_File = result;
        return result;
    }

Update-1 (The byte output is like)

47-30-20-47-36-30-33-0D-0A-41-41-41-3A-0D-0A-5A-3D-49-43-28-2D-35-30-29-0D-0A-5A-3D-49-43-28-35-30-29-0D-0A-47-4F-54-4F-42-20-41-41-41-0D-0A-4D-33-30-0D-0A-0D-0A

推荐答案

Simply reading the bytes will give you the desired string including Windows system newline:

CNC_File_Out = Encoding.UTF8.GetString(buff, 0, buff.length);

No need for BitConverter.ToString() and replacing a null-terminator.

Csharp相关问答推荐

使用 bUnit 测试“禁用”属性

.net6 需要完全限定的类名?

去重流记录

在 MongoDB 和 C# 中查找最后添加的具有某些条件的集合

使用不使用 foreach 的 LINQ 按类别划分产品列表

使用 HttpClient 的 HTTP POST multipart/formdata

如何使用属性在 XML 中插入 node /元素

使用嵌入在字符串中的大括号反序列化 JSON

旧表单仍然显示winforms c#

Linq:将 GroupBy 结果传递给函数

在 Unity Inspector 窗口中获取选定的组件

GetOneDriveUsageAccountDetail() 和 GetSharePointSiteUsageDetail() 在 C# 中不适合我 v1.0

如何在输出目录中获取 C# 库文件?

在 C# 中减去 DateOnly

在 Unity 中使用触发器评分

如何在不指定确切键的情况下读取用户按下的键?

如何根据条件将禁用添加到 html 助手?

如何有效地使用可用内存比较 1,000 张图像

如何将工作分配到计算机池

包含在双打列表中