473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bytes and strings

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
4 1012
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pierre Quentel | last post by:
I want to store strings in a sqlite database, but my strings may contain null bytes and sqlite can't handle this. What is the best way to solve the problem (the fastest in execution time and / or...
2
by: Adam T. Gautier | last post by:
I have been unable to solve a problem. I am working with MD5 signatures trying to put these in a database. The MD5 signatures are not generated using the python md5 module but an external...
17
by: Karl Ebener | last post by:
Hi! I asked a similar question before but then changed everything to using char-Arrays instead of the string class, but I would rather not do this again. So, does anyone know of a...
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
5
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a...
13
by: Pep | last post by:
I have to interface to an older library that uses strings and there is no alternative. I need to pass a string that is padded with null bytes. So how can I append these null bytes to the...
14
by: willie | last post by:
(beating a dead horse) Is it too ridiculous to suggest that it'd be nice if the unicode object were to remember the encoding of the string it was decoded from? So that it's feasible to...
11
by: Gerrit Holl | last post by:
Hi, In Python 3, reading from a file gives bytes rather than characters. Some operations currently performed on strings also make sense when performed on bytes, either if it's binary data or if...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.