Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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/6/cplusplus/136.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.Format# 我是C++开发人员,上周转移到WPF。嗯,我在很多时候都使用了Spavtf在C++应用程序中,现在我已经需要有相当于C语言的东西了。下面是我用C++所做的: char t_str[4] = {}; for(int i = 0; i < 4; i++) { sprintf(t_str, "%02X", buffer[i]); m_apiResponse[i]->setText(String(t_str)); }_C#_C++_.net_Wpf_String - Fatal编程技术网

C# 无法在C中使用String.Format# 我是C++开发人员,上周转移到WPF。嗯,我在很多时候都使用了Spavtf在C++应用程序中,现在我已经需要有相当于C语言的东西了。下面是我用C++所做的: char t_str[4] = {}; for(int i = 0; i < 4; i++) { sprintf(t_str, "%02X", buffer[i]); m_apiResponse[i]->setText(String(t_str)); }

C# 无法在C中使用String.Format# 我是C++开发人员,上周转移到WPF。嗯,我在很多时候都使用了Spavtf在C++应用程序中,现在我已经需要有相当于C语言的东西了。下面是我用C++所做的: char t_str[4] = {}; for(int i = 0; i < 4; i++) { sprintf(t_str, "%02X", buffer[i]); m_apiResponse[i]->setText(String(t_str)); },c#,c++,.net,wpf,string,C#,C++,.net,Wpf,String,其中ResposeBox是我已绑定到的文本框: // Description of Response1Box private string _Response1Box; public string Response1Box { get { return _Response1Box; } set { _Response1Box = value;

其中ResposeBox是我已绑定到的文本框:

// Description of Response1Box
    private string _Response1Box;
    public string Response1Box
    {
        get
        {
            return _Response1Box;
        }

        set
        {
            _Response1Box = value;
            OnPropertyChanged("Response1Box");
        }
    }
缓冲区[64]
是字节[]

它没有给我我的C++方法所期望的答案。这是正确的方法吗:
string bufstring=string.Format(“{0:02}”,buffer[i]);
t_str=Encoding.UTF8.GetBytes(bufstring)我哪里错了

请帮助:)

尝试使用

string bufstring = String.Format("{0:D2}", buffer[i]);
或(用于十六进制输出)

或(带有C/C++样式的十六进制前缀)

看到和

当然,在C++循环中,你调用了<代码> MyAppReal[i] -SETTEXT/Ung>,你应该在C循环中做类似的事情,以获得相同的结果。

string[] response = new TextBox[] {Response1Box, Response2Box, Response3Box, Response4Box};
for (int i = 0; i < 4; i++)
     response[i] = String.Format("{0:X2}", buffer[i]);

我认为你的字节数组逻辑有问题

您有
byte[]t_str=新字节[4]这是一个4字节数组

但是,您多次分配给
t_str
,覆盖原来的4个字节

您可能正在寻找一个声明,如:

byte[][] t_str = new byte[4][];
这将是一个数组的数组

在你的循环中:

t_str[i] = Encoding.UTF8.GetBytes(bufstring);
编辑:

或者如果我偏离了目标,你可以:

Response1Box = buffer[0].ToString("X2");
Response2Box = buffer[1].ToString("X2");
Response3Box = buffer[2].ToString("X2");
Response4Box = buffer[3].ToString("X2");

假设
buffer
是一个字节数组,您想写出每个字节的值

以下是C#中可能需要的所有字符串格式 我想你可能需要以下东西

String str = String.Format("Hello {0:0.00} {1:0.00}", 55, 20);

那么,你期待的答案/响应是什么?…所以,如果我记得我的C++,你想要0个填充的2位十六进制?这对你可能有用:感谢史提夫的回复:)响应基本上是一个字符串。由于使用了TextBox[],其抛出错误:)在响应1,2,3,4box处抛出错误,因为它是TextBox属性。检查我的更新问题:)
byte[][] t_str = new byte[4][];
t_str[i] = Encoding.UTF8.GetBytes(bufstring);
Response1Box = buffer[0].ToString("X2");
Response2Box = buffer[1].ToString("X2");
Response3Box = buffer[2].ToString("X2");
Response4Box = buffer[3].ToString("X2");
String str = String.Format("Hello {0:0.00} {1:0.00}", 55, 20);