How to convert int to byte[]

  • Thread starter Thread starter Guest
  • Start date Start date
Shankar said:
How can we convert the integer values to byte arrays using C#.NET

Depending on exactly what you want, BitConverter is usually the way to
go.
 
avnrao said:
Encoding.UTF8.GetBytes(i.ToString());

While that will indeed convert an int into an array of bytes, I'm not
sure it's likely to be the format the OP is looking for - I suspect
BitConverter is more likely to be useful.
 
Yes, it was useful. But again i got one problem of using this UTF8

1. I have one string that i'm encrypting and getiing byte[]
but in SQL data base i have the field type varchar for that one i converting
that
byte[] to string and storing database. when I'm going to Decrypt that string
i'm getting length problem.

guide me in this regard.
 
shankar said:
Yes, it was useful. But again i got one problem of using this UTF8

1. I have one string that i'm encrypting and getiing byte[]
but in SQL data base i have the field type varchar for that one i converting
that
byte[] to string and storing database. when I'm going to Decrypt that string
i'm getting length problem.

Are you trying to convert the byte[] to a string just by using
Encoding.UTF8.GetString()? You shouldn't - you should use base 64 to
convert *arbitrary* binary data to a string and back again. See
Convert.ToBase64String and Convert.FromBase64String.
 
Back
Top