所以即时尝试制作一个字节转换器C# [英] So im trying to make a byte to byte converter C#

查看:108
本文介绍了所以即时尝试制作一个字节转换器C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bsically我有一个按钮和两个富文本框,我试图将0x和,添加到我的十六进制字节,例如从00 01 02到0x00,0x01,0x02我的问题是我一直得到一个对象引用未设置为对象错误的实例,这是我的代码



  public   int  i { get ;  set ; } 
public int 金额{ get ; set ; }


private void button2_Click( object sender,EventArgs e)
{
if (richTextBox1.Text.Length > = 2
{


string text = richTextBox1.Text.Replace( ,< span class =code-string>
);
int num = text.Length;
for int i = 0 ; i < = num; i + = 6
{
num = text.Length + 2 ;
text = text.Insert(i,( 0x));
text = text.Insert(i + 4 ,( ));
}


richTextBox2.Text = text.Remove(text.Length - 2 );
}







i我在这里收到错误,如果(richTextBox1。 Text.Length> = 2)



这里是完整的错误





字节converter.exe中发生类型为'System.NullReferenceException'的未处理异常



附加信息:对象引用未设置为对象的实例。< br $>






顺便说一下我是一个初学者编码器所以对我来说很容易大声笑,任何人都会非常感激。

解决方案

首先检查调试器:如CPallini所说,你的richTextBox1可能不是一个有效的控件 - 这可能意味着你在代码中创建了它而不是通过设计师分配它。因此它也不会显示!



查看设计器,突出显示您认为将获得用户输入的富文本框,并查看属性窗格找到它的名字。在你的Click事件处理程序中使用它,而不是richTextBox1,它可能会开始工作。



但是......你觉得这段代码怎么办?

  string  text = richTextBox1.Text.Replace(  ); 

为什么要尝试用空字符串替换空字符串?

用于插入0x和逗号的代码有点奇怪同样:考虑使用StringBuilder,或者最好先将输入字符串分解为十六进制数字,然后将它们转换为0xNN并使用String.Join添加所有逗号 - 这是一个更简洁的解决方案阅读! :笑:


bsically i have one button and two rich text boxes, i am trying to add "0x" and a "," to my hex bytes, example from 00 01 02 to 0x00, 0x01, 0x02 my problem is i keep getting a Object reference not set to an instance of an object error, here is my code

public int i { get; set; }
     public int amount { get; set; }


     private void button2_Click(object sender, EventArgs e)
     {
         if (richTextBox1.Text.Length >= 2)
         {


             string text =  richTextBox1.Text.Replace( "", "");
             int num = text.Length;
             for (int i = 0; i <= num; i += 6)
             {
                 num = text.Length + 2;
                 text = text.Insert(i,( "0x"));
                 text = text.Insert(i + 4,( ","));
             }


             richTextBox2.Text = text.Remove(text.Length - 2);
         }




i am getting the error here, if (richTextBox1.Text.Length >= 2)

here is the full error


An unhandled exception of type 'System.NullReferenceException' occurred in byte converter.exe

Additional information: Object reference not set to an instance of an object.



btw im a beginner coder so be easy on me lol, any helf would be greatly appreciated.

解决方案

Start by checking in the debugger: as CPallini says, it's likely that your richTextBox1 is not a valid control - which presumably means you created it in your code instead of allocating it via the designer. So it won't be displayed either!

Look at the designer, highlight the rich text box you think is going to get the user input, and look at the properties pane to find it's name. Use that in your Click event handler, instead of richTextBox1 and it may start to work.

But... what do you think this code does?

string text =  richTextBox1.Text.Replace( "", "");

Why are you trying to replace an empty string with an empty string?
The code you are using to insert your "0x" and commas is a little odd as well: Consider using a StringBuilder instead, or better break your input string into the hex digits first, then convert them to "0xNN" and use String.Join to add all the commas - it's a "cleaner" solution that is a lot easier to read! :laugh:


这篇关于所以即时尝试制作一个字节转换器C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆