views:

48

answers:

2

Well my question is simple and straightforward.

Is there any way we can use hex values like in c++?

I am going to write binary files, but for that i will have to define certain characters like this for example.

\x00\x00\x11\x22\x33\x00\x00

I would first need to convert stuff like this to a byte array, and then write it to a binary text file.

Thanks!

+3  A: 

No, that's a problem with modern compilers, like VB.NET's. There is no one-to-one mapping between bytes and strings anymore when Unicode became the preferred way of handling text. Codepoints like 0x80 don't have a corresponding character, it is going to get munched when you convert the string to bytes.

You'll need to work with a Byte() array in your code. The exact equivalent for your example is:

    Dim data As Byte() = {&H0, &H0, &H11, &H22, &H33, &H0, &H0}
Hans Passant
+1 @zeta, you've referred to a *binary text file* in your question. If it's a *binary* file, you need to treat it like a binary file - don't try to abuse strings to hold binary data.
MarkJ
A: 

There's another post here about converting text HEX char strings to a byte array.

http://stackoverflow.com/questions/321370/convert-hex-string-to-byte-array

Put that in an extension method that extends the STRING class:

http://msdn.microsoft.com/en-us/library/bb384936.aspx

And you could end up with a line of code looking like this:

Dim Bytes() = "\x00\x00\x11\x22\x33\x00\x00".ToBytes

Which is pretty dang close to feeling built in.

drventure