Convert int to byte [] and vice versa
-
Hi all, i'm trying to write some Socket code in C#. I'm still learning all the intricacies of C#, and have come across something I'm not sure how to do. It may have been asked here before but I've failed to find any info on, so any help would be appreciated. Socket.Send accepts a byte []. If I want to send an "int" which is the length of the bytes that I will be sending next, is there a conversion function from "int" to "byte []"? The same goes for Socket.Receive. Is there a function that converts a "byte []" into an "int"? Thanx If there's one thing I've learned, it's that life is one crushing defeat after another until you just wish Flanders was dead. - Homer Simpson
-
Hi all, i'm trying to write some Socket code in C#. I'm still learning all the intricacies of C#, and have come across something I'm not sure how to do. It may have been asked here before but I've failed to find any info on, so any help would be appreciated. Socket.Send accepts a byte []. If I want to send an "int" which is the length of the bytes that I will be sending next, is there a conversion function from "int" to "byte []"? The same goes for Socket.Receive. Is there a function that converts a "byte []" into an "int"? Thanx If there's one thing I've learned, it's that life is one crushing defeat after another until you just wish Flanders was dead. - Homer Simpson
you could try this - byte[] myByteArray = (byte[])intValue; Gavirj
-
Hi all, i'm trying to write some Socket code in C#. I'm still learning all the intricacies of C#, and have come across something I'm not sure how to do. It may have been asked here before but I've failed to find any info on, so any help would be appreciated. Socket.Send accepts a byte []. If I want to send an "int" which is the length of the bytes that I will be sending next, is there a conversion function from "int" to "byte []"? The same goes for Socket.Receive. Is there a function that converts a "byte []" into an "int"? Thanx If there's one thing I've learned, it's that life is one crushing defeat after another until you just wish Flanders was dead. - Homer Simpson
The
BitConverter
class does what your looking for.
website // Project : AmmoITX //profile Another Post by NnamdiOnyeyiri
-
you could try this - byte[] myByteArray = (byte[])intValue; Gavirj
Gavin, that didn't work. I get the following error :
(136): Cannot convert type 'int' to 'byte[]'
Bute Nnamdi helped me, I needed to use the BitConverter class and it has a ToInt32 and GetBytes functions. If there's one thing I've learned, it's that life is one crushing defeat after another until you just wish Flanders was dead. - Homer Simpson