create hex-string to send on networkstream

  • Thread starter Thread starter Sven Huijbrechts
  • Start date Start date
S

Sven Huijbrechts

Hello,

I need to send a couple of "NUL"s -> HEX value "00" on a networkstream..

This is the code we currently use to send string:
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(inputtext.ToString)
networkStream.Write(sendBytes, 0, sendBytes.Length)


Since there is no char value for hex 00 (NUL) it's not possible to use a
string as input..


Thanks,
Sven
 
Sven Huijbrechts said:
I need to send a couple of "NUL"s -> HEX value "00" on a
networkstream..

This is the code we currently use to send string:
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(inputtext.ToString)
networkStream.Write(sendBytes, 0, sendBytes.Length)


Since there is no char value for hex 00 (NUL) it's not possible to
use a string as input..

Why is there no "char value"? chr(0) returns such a char. Don't use ASCII
encoding because ASCII = 7 bits. Do you want to send the character codes of
the hexadecimal string representation of the chars, or do you want to send
the character codes themselves? What is "inputtext"? What does it contain
for example?

One possible solution:
Dim sendBytes As Byte() 'no [ ] required here
sendBytes = Encoding.Default.GetBytes(inputtext.ToString)
networkStream.Write(sendBytes, 0, sendBytes.Length)

Encoding.Default returns the current ANSI codepage (8-Bit character codes).

The other solution depends on the answers to the questions above.

Maybe you can use a Streamwriter instead. You could pass the encoding once
to the constructor and don't have to convert each string on your own.
 
When you look at this page:
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/_pluslang_ascii_character_codes_ch
art_1.htm

The first line (Dec: 0 / Hex: 00 / Char: / Code: NUL) is the code I have to
send..
I need to send a bigendian HEX value over the networkstream.
So I want to fill the array sendBytes with '00','00','00','3E' for example.

My problem is that the string '0000003E' is converted to hex
'30','30','30','30','30','30','33','45'
And there is no string which translates to hex '00','00','00','3E'

Sven

Armin Zingler said:
Sven Huijbrechts said:
I need to send a couple of "NUL"s -> HEX value "00" on a
networkstream..

This is the code we currently use to send string:
Dim sendBytes As [Byte]() =
Encoding.ASCII.GetBytes(inputtext.ToString)
networkStream.Write(sendBytes, 0, sendBytes.Length)


Since there is no char value for hex 00 (NUL) it's not possible to
use a string as input..

Why is there no "char value"? chr(0) returns such a char. Don't use ASCII
encoding because ASCII = 7 bits. Do you want to send the character codes of
the hexadecimal string representation of the chars, or do you want to send
the character codes themselves? What is "inputtext"? What does it contain
for example?

One possible solution:
Dim sendBytes As Byte() 'no [ ] required here
sendBytes = Encoding.Default.GetBytes(inputtext.ToString)
networkStream.Write(sendBytes, 0, sendBytes.Length)

Encoding.Default returns the current ANSI codepage (8-Bit character codes).

The other solution depends on the answers to the questions above.

Maybe you can use a Streamwriter instead. You could pass the encoding once
to the constructor and don't have to convert each string on your own.

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Sven Huijbrechts said:
When you look at this page:
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/_pluslang_ascii_character_codes_ch
art_1.htm

The first line (Dec: 0 / Hex: 00 / Char: / Code: NUL) is the code I
have to send..
I need to send a bigendian HEX value over the networkstream.
So I want to fill the array sendBytes with '00','00','00','3E' for
example.

My problem is that the string '0000003E' is converted to hex
'30','30','30','30','30','30','33','45'
And there is no string which translates to hex '00','00','00','3E'

You say the string to be sent is "0000003E"? If yes:


Dim s As String = "0000003E"
Dim b As Byte()
Dim i As Integer

ReDim b(s.Length \ 2 - 1)

For i = 0 To s.Length Step 2
b(i \ 2) = Convert.ToByte(s.Substring(i, 2), 16)
Next

If it is always 8 chars in the source string (representing an Integer):

b = BitConverter.GetBytes(Convert.ToInt32(s, 16)).
 
Sven Huijbrechts said:
When you look at this page:
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/_pluslang_ascii_character_codes_ch
art_1.htm

The first line (Dec: 0 / Hex: 00 / Char: / Code: NUL) is the code I
have to send..
I need to send a bigendian HEX value over the networkstream.
So I want to fill the array sendBytes with '00','00','00','3E' for
example.

My problem is that the string '0000003E' is converted to hex
'30','30','30','30','30','30','33','45'
And there is no string which translates to hex '00','00','00','3E'

You say the string to be sent is "0000003E"? If yes:


Dim s As String = "0000003E"
Dim b As Byte()
Dim i As Integer

ReDim b(s.Length \ 2 - 1)

For i = 0 To s.Length Step 2
b(i \ 2) = Convert.ToByte(s.Substring(i, 2), 16)
Next

If it is always 8 chars in the source string (representing an Integer):

b = BitConverter.GetBytes(Convert.ToInt32(s, 16)).


......


I forgot: In the first example the byte order must be reversed.
To reverse an existing array, use Array.Reverse.
 
thanks for the info..
I'll have a look at it soon, currently other priorities..

Greetings,
Sven
 
Back
Top