problem with hexa

  • Thread starter Thread starter Natan Vivo
  • Start date Start date
N

Natan Vivo

Ok, let me explain this first. I did a search about 'hexadecimal'
first and found a lot of threads about it, but none of them tell me
what i want to know.

I am new to C#, and want to work with binary data (actually some
custom file formats). I used to do this a lot in PHP, but i can't
understand how this works in C#.

in PHP i could do this:

$packet = 0x2a . 0x00 . 0x00 . 0x3f . "some string";

it would result in a var with 4 bytes that i specified exacly, and a
string. A simple binary data packet.

in C#, if i try

var = 0x00 + "some string";

when i save this to a file, 0x00 turns to 0x30

all i want is to create a var like i did in the PHP example, but in
the C# way..... anyone can help me?

i don't know if it's the right way to do it in this language, please
if you have a better idea, tell me. =)
 
Hi Natan,

Try this:
UnicodeEncoding unicode = new UnicodeEncoding();
string s = unicode.GetString(new byte[]{your bytes here})

Note that there are others encoders, such as ASCIIEncoding, etc.
 
Natan Vivo said:
Ok, let me explain this first. I did a search about 'hexadecimal'
first and found a lot of threads about it, but none of them tell me
what i want to know.

I am new to C#, and want to work with binary data (actually some
custom file formats). I used to do this a lot in PHP, but i can't
understand how this works in C#.

in PHP i could do this:

$packet = 0x2a . 0x00 . 0x00 . 0x3f . "some string";

it would result in a var with 4 bytes that i specified exacly, and a
string. A simple binary data packet.

That's not really simple at all - you need to work out what text encoding
you mean, to start with.

Don't try to turn your binary data into a string - instead, deal with
the file at a binary level and convert your string into a byte array
with the appropriate Encoding.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 
Jon Skeet said:
That's not really simple at all - you need to work out what text encoding
you mean, to start with.

Don't try to turn your binary data into a string - instead, deal with
the file at a binary level and convert your string into a byte array
with the appropriate Encoding.

I am not working with a file. And i don't want to work with
encodings.... See, eg: I want to create a var and put 0x00 x0x2f 0x00
0x20 and "something" in.

Ok, "something" is ascii... but 0x00 and etc, there is no encoding in
this. it is a byte and i should not have to use anything to convert,
encode, etc... it is what it is.

I would like to understand how it works in C#... i can't understand
how to work correctly with a byte array. how to add, remove, and
convert everything into a single text.

I have no use for unicode of filestreams in my program. =/
 
Miha Markic said:
Hi Natan,

Try this:
UnicodeEncoding unicode = new UnicodeEncoding();
string s = unicode.GetString(new byte[]{your bytes here})

Note that there are others encoders, such as ASCIIEncoding, etc.

I can't understand why i should encode something... i am telling
exactly what bytes i want to store.

Man, this C# is getting me headaches... i'll probably go right back to
C...
 
Natan Vivo said:
I am not working with a file.

Then work with a memory stream, or a byte array.
And i don't want to work with
encodings.... See, eg: I want to create a var and put 0x00 x0x2f 0x00
0x20 and "something" in.

Ok, "something" is ascii...

Ah, so you want Encoding.ASCII.
but 0x00 and etc, there is no encoding in
this. it is a byte and i should not have to use anything to convert,
encode, etc... it is what it is.

Exactly - you deal with everything in binary, including your string,
which you convert to binary with an Encoding.
I would like to understand how it works in C#... i can't understand
how to work correctly with a byte array. how to add, remove, and
convert everything into a single text.

The easiest way is probably to use a binary stream. You need to be
careful though - you say you want to "convert everything into a single
text" but the binary data *isn't* text, it's binary!
I have no use for unicode of filestreams in my program. =/

But you *do* have use for an encoding, namely Encoding.ASCII.
 
Natan Vivo said:
I can't understand why i should encode something... i am telling
exactly what bytes i want to store.

You need to encode your string, because you want it represented as
bytes. You need to understand that a string is a sequence of
characters, *not* bytes.
Man, this C# is getting me headaches... i'll probably go right back to
C...

It's actually just the same in C, but a lot of C programmers don't
realise it - they rarely deal with encodings properly. (Things might
well be better now than they were, but basically treating text as if it
were already binary is a bad move.)
 
Back
Top