By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,890 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,890 IT Pros & Developers. It's quick & easy.

Bytes and strings

P: n/a
Just a detail about correctness/elegance (I think!) but it would be
useful to know for future reference:

I need to read a pre-existing binary file that contains as one of its
elements a 16-byte non-unicode string, which I need to assemble into a
..Net string for further processing. It seems like my only option for
reading the bytes from the file is to read them into a byte array. OK
well I can do this with a Binary Reader:

MyByteArray = BR.readbytes(16)

But is there then a more concise way of assembling the string?
Currently I'm doing:

For i as integer = 0 to 15
MyString &= chr(MyByteArray(i))
Next i

But is there a better way? Or are any alternatives not one-liners
either?

TIA
John Dann
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bob
Have a look at System.BitConverter.

Bob

"John Dann" <ne**@prodata.co.uk> wrote in message
news:3m********************************@4ax.com...
Just a detail about correctness/elegance (I think!) but it would be
useful to know for future reference:

I need to read a pre-existing binary file that contains as one of its
elements a 16-byte non-unicode string, which I need to assemble into a
.Net string for further processing. It seems like my only option for
reading the bytes from the file is to read them into a byte array. OK
well I can do this with a Binary Reader:

MyByteArray = BR.readbytes(16)

But is there then a more concise way of assembling the string?
Currently I'm doing:

For i as integer = 0 to 15
MyString &= chr(MyByteArray(i))
Next i

But is there a better way? Or are any alternatives not one-liners
either?

TIA
John Dann

Nov 21 '05 #2

P: n/a
John,
But is there a better way? Or are any alternatives not one-liners
either? Yes there is a significantly better way.

Use a System.Text.Encoding object.

For example:

MyString = System.Text.Encoding.Default.GetString(MyByteArray )

Which will convert the byte array into a string based on the current Windows
Code Page as defined in the Regional settings in Control Panel.

Hope this helps
Jay

"John Dann" <ne**@prodata.co.uk> wrote in message
news:3m********************************@4ax.com... Just a detail about correctness/elegance (I think!) but it would be
useful to know for future reference:

I need to read a pre-existing binary file that contains as one of its
elements a 16-byte non-unicode string, which I need to assemble into a
.Net string for further processing. It seems like my only option for
reading the bytes from the file is to read them into a byte array. OK
well I can do this with a Binary Reader:

MyByteArray = BR.readbytes(16)

But is there then a more concise way of assembling the string?
Currently I'm doing:

For i as integer = 0 to 15
MyString &= chr(MyByteArray(i))
Next i

But is there a better way? Or are any alternatives not one-liners
either?

TIA
John Dann

Nov 21 '05 #3

P: n/a
Bob,
Did you try BitConvert.ToString?

It converts the bytes to numbers then concatenates these together...

for example:

Dim bytes() As Byte = {&H42, &H6F, &H62}

Debug.WriteLine(BitConverter.ToString(bytes), "bitconverter")
Debug.WriteLine(System.Text.Encoding.Default.GetSt ring(bytes),
"encoding")

Hope this helps
Jay
"Bob" <no***@nowhere.com> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
Have a look at System.BitConverter.

Bob

"John Dann" <ne**@prodata.co.uk> wrote in message
news:3m********************************@4ax.com...
Just a detail about correctness/elegance (I think!) but it would be
useful to know for future reference:

I need to read a pre-existing binary file that contains as one of its
elements a 16-byte non-unicode string, which I need to assemble into a
.Net string for further processing. It seems like my only option for
reading the bytes from the file is to read them into a byte array. OK
well I can do this with a Binary Reader:

MyByteArray = BR.readbytes(16)

But is there then a more concise way of assembling the string?
Currently I'm doing:

For i as integer = 0 to 15
MyString &= chr(MyByteArray(i))
Next i

But is there a better way? Or are any alternatives not one-liners
either?

TIA
John Dann


Nov 21 '05 #4

P: n/a
Bob
Heh heh oops... brain misfire. :)
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oy**************@tk2msftngp13.phx.gbl...
Bob,
Did you try BitConvert.ToString?

It converts the bytes to numbers then concatenates these together...

for example:

Dim bytes() As Byte = {&H42, &H6F, &H62}

Debug.WriteLine(BitConverter.ToString(bytes), "bitconverter")
Debug.WriteLine(System.Text.Encoding.Default.GetSt ring(bytes),
"encoding")

Hope this helps
Jay

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.