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