problem with hexa

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Natan Vivo

    #1

    problem with hexa

    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. =)
  • Miha Markic

    #2
    Re: problem with hexa

    Hi Natan,

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

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

    --
    Miha Markic - RightHand .NET consulting & software development
    miha at rthand com


    "Natan Vivo" <nvivo@mandic.c om.br> wrote in message
    news:82c33881.0 311140354.5218a c9@posting.goog le.com...[color=blue]
    > 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. =)[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: problem with hexa

      Natan Vivo <nvivo@mandic.c om.br> wrote:[color=blue]
      > 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.[/color]

      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 - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Natan Vivo

        #4
        Re: problem with hexa

        Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in message news:<MPG.1a1ee 6233a99fdf989aa a@msnews.micros oft.com>...[color=blue]
        > 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.[/color]

        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.
        [color=blue]
        > See http://www.pobox.com/~skeet/csharp/unicode.html for more
        > information.[/color]

        I have no use for unicode of filestreams in my program. =/

        Comment

        • Natan Vivo

          #5
          Re: problem with hexa

          "Miha Markic" <miha at rthand com> wrote in message news:<#WDTR$qqD HA.1444@tk2msft ngp13.phx.gbl>. ..[color=blue]
          > Hi Natan,
          >
          > Try this:
          > UnicodeEncoding unicode = new UnicodeEncoding ();
          > string s = unicode.GetStri ng(new byte[]{your bytes here})
          >
          > Note that there are others encoders, such as ASCIIEncoding, etc.[/color]

          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...

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: problem with hexa

            Natan Vivo <nvivo@mandic.c om.br> wrote:[color=blue]
            > Jon Skeet [C# MVP] <skeet@pobox.co m> wrote:[color=green]
            > > 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.[/color]
            >
            > I am not working with a file.[/color]

            Then work with a memory stream, or a byte array.
            [color=blue]
            > 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...[/color]

            Ah, so you want Encoding.ASCII.
            [color=blue]
            > 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.[/color]

            Exactly - you deal with everything in binary, including your string,
            which you convert to binary with an Encoding.
            [color=blue]
            > 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.[/color]

            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!
            [color=blue][color=green]
            > > See http://www.pobox.com/~skeet/csharp/unicode.html for more
            > > information.[/color]
            >
            > I have no use for unicode of filestreams in my program. =/[/color]

            But you *do* have use for an encoding, namely Encoding.ASCII.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: problem with hexa

              Natan Vivo <nvivo@mandic.c om.br> wrote:[color=blue][color=green]
              > > Note that there are others encoders, such as ASCIIEncoding, etc.[/color]
              >
              > I can't understand why i should encode something... i am telling
              > exactly what bytes i want to store.[/color]

              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.
              [color=blue]
              > Man, this C# is getting me headaches... i'll probably go right back to
              > C...[/color]

              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.)

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...