Problerm on the conversion between int32 and byte[]

  • Thread starter Thread starter Stephanie Yao via .NET 247
  • Start date Start date
S

Stephanie Yao via .NET 247

Hi, I've done something on the conversion between int32 and byte[],here is my code:

1, I convert byte[] to an int:
int regInt = BitConverter.ToInt32(regBytes, 0);
this.tbRID.Text = regInt.ToString;//show int to the textbox

2, I convert the int back to byte[]:
int regInt = int.Parse(this.tbRID.Text);//get int from the textbox
byte[] regBytes = BitConverter.GetBytes(regInt);

But I found some data missed. The regBytes.Length should be 32, but here it's only 4.

Hope someone could help me.
 
Stephanie Yao via .NET 247 said:
Hi, I've done something on the conversion between int32 and byte[],here is my code:

1, I convert byte[] to an int:
int regInt = BitConverter.ToInt32(regBytes, 0);
this.tbRID.Text = regInt.ToString;//show int to the textbox

2, I convert the int back to byte[]:
int regInt = int.Parse(this.tbRID.Text);//get int from the textbox
byte[] regBytes = BitConverter.GetBytes(regInt);

But I found some data missed. The regBytes.Length should be 32, but here it's only 4.

Why would it be 32? It's getting *bytes*, not *bits*. Look at the docs
for BitConverter.GetBytes(Int32):

<quote>
Return Value
An array of bytes with length 4.
</quote>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top