Data Conversion

J

Jack

Simple question:

Does anyone know how to convert integer values to character strings that can
written to binary files? For example -1 = Hex(FFFF) etc..
 
J

Jay B. Harlow [MVP - Outlook]

Jack,
If I needed to write integers to a binary file, I would simply write the
integer, as it is already Binary!

Have a look at the System.IO.BinaryReader & System.IO.BinaryWriter classes.
They allow you to read & write binary files, there are overloaded Write
methods that will read & write integers.

Your example is demonstrating how to convert an integer into Hex digits, hex
digits are not binary per se. You can use Convert.ToString(-1s, 16) to
convert a Short -1 to hex, likewise you can use Convert.ToInt16(s, 16) to
convert a hex string to a Short. You would use Convert.ToInt32 to convert to
an Integer.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

Jack,
I should add the System.BitConverter class is useful to convert values to &
from an array of bytes.

Where using a BinaryReader or BinaryWriter is not specifically appropriate.

Dim bytes() As Byte
Dim i As Integer
bytes = BitConverter.GetBytes(i)
i = BitConverter.ToInt32(bytes)
A helpful dose of the (should be) obvious.
I didn't think so, as the Framework is HUGE, its hard to learn your way
around. I think its even harder for some to learn learn when to use which
when the .NET offers seemingly the same way to do something. Such as
Microsoft.VisualBasic.Strings.Split, System.String.Split, and
System.Text.RegularExpresssions.Split.

Hope this helps
Jay
 

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