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相关问答推荐

无法从具有一对多关系的C#类中使用Swagger创建记录

C#类主构造函数中的调试参数

如何使用C#中的图形API更新用户配置文件图像

我需要两个属性类吗

如果存在对CodeAnalysis.CSharp的引用,则不能引用netStandard2.0库

从应用程序图API调用访问所有者字段

使用Audit.EntityFramework,我如何将外键的值设置为相关实体上的属性?

为什么Regex.IsMatch(";\\t";,";\\t";)返回FALSE而不是TRUE?

CS1660无法将lambda表达式转换为类型INavigationBase,因为它不是委托类型

如何使用MoQ模拟Resources GroupCollection?

Docker Container中的HttpRequest后地址不可用

将类移动到新命名空间后更新RavenDB Raven-Clr-Type

从另一个不同 struct 的数组创建Newtonsoft.Json.Linq.J数组

GODOT 4向C#中的字符串参数发送信号以等待

当空判断结果赋给变量时,为什么会出现可能空异常警告的解引用?

源代码生成器:CS8795分部方法';Class1.GetS2(字符串)';必须有实现部分,因为它有可访问性修饰符?

在C#中通过Matheval使用自定义公式

如何在JSON:API中定义的&过滤查询参数系列&标准的GET请求中传递多个相关参数?

C#如何替换两个XML元素值?

C#11/.NET 7-判断&t;值类型>;后的可为空性行为