Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Convert int to byte [] and vice versa

Convert int to byte [] and vice versa

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphelptutorialquestionlearning
4 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vadim Tabakman
    wrote on last edited by
    #1

    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

    G N 2 Replies Last reply
    0
    • V Vadim Tabakman

      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

      G Offline
      G Offline
      Gavin Jeffrey
      wrote on last edited by
      #2

      you could try this - byte[] myByteArray = (byte[])intValue; Gavirj

      V 1 Reply Last reply
      0
      • V Vadim Tabakman

        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

        N Offline
        N Offline
        Nnamdi Onyeyiri
        wrote on last edited by
        #3

        The BitConverter class does what your looking for.


        website // Project : AmmoITX //profile Another Post by NnamdiOnyeyiri

        1 Reply Last reply
        0
        • G Gavin Jeffrey

          you could try this - byte[] myByteArray = (byte[])intValue; Gavirj

          V Offline
          V Offline
          Vadim Tabakman
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups