Two byte in one string?

B

Bojan

Hello,
how can I put two byte in one String , and I want that this one byte be
behind second?


Like this : Dim b1,b2 As Byte ("b1=2D") ("b2=52")
Dim s As String = b1&b2 ("2D52")

Did this code put first byte behaind second in "str" (string)?
 
M

Mattias Sjögren

how can I put two byte in one String , and I want that this one byte be
behind second?

Are you looking for

Dim s As String = b1.ToString("X2") & b2.ToString("X2")


Mattias
 
B

Bojan

Yes,
but I have one more question. Can I put second byte in different time in
string? Because, I have situation like this, I read two byte from serial
port, with distance, and because I nead put first byte diferend on second in
string.

Mattias:
What is "X" in Yours code? What this mean?

Thank you !
Bojan
 
M

Morten Wennevik

Hi Bojan,

You can assemble the string any way you want. The X2 means output as 2-digit hexadecimal value.


Yes,
but I have one more question. Can I put second byte in different time in
string? Because, I have situation like this, I read two byte from serial
port, with distance, and because I nead put first byte diferend on second in
string.

Mattias:
What is "X" in Yours code? What this mean?

Thank you !
Bojan
 
M

Mattias Sjögren

Can I put second byte in different time in string?

Sure, just use the & operator to append the new string.

Dim s As String = b1.ToString("X2")
....
s = s & b2.ToString("X2")

If you actually have many more bytes, you should consider using the
System.Text.StringBuilder class instead.

What is "X" in Yours code? What this mean?

It means that the number should be formatted with upper-case
hexadecimal digits. See
http://msdn2.microsoft.com/en-us/library/dwhawy9k.aspx


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top