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. Visual Basic
  4. PLEASE HELP: Converting a integer (or long) to a Binary Byte

PLEASE HELP: Converting a integer (or long) to a Binary Byte

Scheduled Pinned Locked Moved Visual Basic
csharpdata-structureshelptutorial
2 Posts 2 Posters 0 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.
  • L Offline
    L Offline
    Lola Copinga
    wrote on last edited by
    #1

    PLEASE HELP I have a requirement to send messages over TCPIP. The format of this message is 2 bytes for a binary length of the entire message, 6 spaces and then the message. The length of these messages can be as least 66 bytes and up to 5500 bytes. I had this working in VB6, but now need to re-write it in .net. The code I used in VB6 is as follows: Public Function buildIPheader(lgth As Long) As String 'set the length of the TCP/IP header 'add 8 bytes for the IP header lgth = lgth + 8 'IP Header = 8 bytest, first two bytes are length of record, last 6 bytes are unused. 'compute the length into binary format Dim comp1, comp2 comp1 = lgth \ 256 comp2 = lgth Mod 256 '2 bytes binary for the length and 6 bytes spaces for future use buildIPheader = StrConv(ChrB(comp1), vbUnicode) & _ StrConv(ChrB(comp2), vbUnicode) & _ " " End Function As you know the chrB and vbUnicode are no longer supported under .net. I have tried the encodeing getbytes method but that creates an array which I can't use. Does anyone know how to do this using VB.NET??

    N 1 Reply Last reply
    0
    • L Lola Copinga

      PLEASE HELP I have a requirement to send messages over TCPIP. The format of this message is 2 bytes for a binary length of the entire message, 6 spaces and then the message. The length of these messages can be as least 66 bytes and up to 5500 bytes. I had this working in VB6, but now need to re-write it in .net. The code I used in VB6 is as follows: Public Function buildIPheader(lgth As Long) As String 'set the length of the TCP/IP header 'add 8 bytes for the IP header lgth = lgth + 8 'IP Header = 8 bytest, first two bytes are length of record, last 6 bytes are unused. 'compute the length into binary format Dim comp1, comp2 comp1 = lgth \ 256 comp2 = lgth Mod 256 '2 bytes binary for the length and 6 bytes spaces for future use buildIPheader = StrConv(ChrB(comp1), vbUnicode) & _ StrConv(ChrB(comp2), vbUnicode) & _ " " End Function As you know the chrB and vbUnicode are no longer supported under .net. I have tried the encodeing getbytes method but that creates an array which I can't use. Does anyone know how to do this using VB.NET??

      N Offline
      N Offline
      Niels Penneman
      wrote on last edited by
      #2

      Why do people ALWAYS FORGET that unicode takes TWO BYTES for ONE CHARACTER?? :-) If you would change the encoding from ASCII to Unicode in the example below, you would only get one char and six spaces... And if you write new programs in .NET i suggest you use UTF-8 encoding (1 OR 2 bytes / char) Use ASCII encoding if you want only 8 bytes:

      Public Function buildIPheader(ByVal Length As Integer) As String
      
          'set the length of the TCP/IP header
          'add 8 bytes for the IP header
      
          Length += 8
      
          'IP Header = 8 bytes, first two bytes are length of record, last 6 bytes are unused
      
          'compute the length into binary format
          Dim comp1, comp2 As Byte
          comp1 = Length \\ 256
          comp2 = Length Mod 256
      
          '2 bytes binary for the length and 6 bytes spaces for future use
          Return System.Text.Encoding.ASCII.GetString(New Byte() {comp1, comp2}) & "      "
      
      End Function
      

      However, in .NET you should (better) work with byte arrays and then the code looks like this :-) :

      Public Function buildIPheader(ByVal Length As Integer) As Byte()
      
          'set the length of the TCP/IP header
          'add 8 bytes for the IP header
      
          Length += 8
      
          'IP Header = 8 bytes, first two bytes are length of record, last 6 bytes are unused
      
          'compute the length into binary format
          '2 bytes binary for the length and 6 bytes spaces for future use
          Dim header(7) As Byte
          header(0) = Length \\ 256
          header(1) = Length Mod 256
          System.Text.Encoding.ASCII.GetBytes("      ", 0, 6, header, 2)
      
          Return header
      
      End Function
      

      And as the other six bytes are unused, I wouldn't even set them, or set them to zero :-) greetz ;-) *Niels Penneman*


      Software/Dev Site
      Personal Site


      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