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