开发者

Input string was not in a correct format?

In my application i write the code like thi开发者_如何学Gos

byte[] byt = new byte[Convert.ToSbyte(textbox1.Text)];

it is giving the error that input string was not in a correct format.


This is a wild guess, but are you trying to convert the contents of the text box into a byte array? If so, you can do it like this:

byte[] byt = Encoding.UTF8.GetBytes(textbox1.Text);


The text in textbox1 is not a valid numeral for a signed byte.

Does it have spaces? Letters? ...?


What are you trying to do? The new byte[num] creates an array of 'num' bytes, where 'num' is usually an integer. All bytes in the array are then 0.

It doesn't create a filled array, as I suspect you may be trying to do.

What are the contents of that textbox1.Text that gave the error?


what you want in fact is this

 // C# to convert a string to a byte array.
 public static byte[] StrToByteArray(string str)
 {
     System.Text.ASCIIEncoding  encoding=new System.Text.ASCIIEncoding();
     return encoding.GetBytes(str);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜