displaying unicode encodings (ie 0x73 type notation)

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

    displaying unicode encodings (ie 0x73 type notation)

    I'm trying to write a basic tool to convert strings to unicode
    encodings. Should be easy enough, I can do the encoding bit with the
    various encoding tools in C#, but what I can't seem to do is force C#
    to spit out the encodings - everything I've done just seems to decode
    the unicode and spit out exactly what I had typed in. How do I go from
    a byte[] of unicode bytes to a string?

    (As you might guess, I'm not really much of a coder - even when I was,
    dealing with characters was the part of coding I hated. Gimme numbers
    any day...)

    Greg
  • Jon Skeet [C# MVP]

    #2
    Re: displaying unicode encodings (ie 0x73 type notation)

    Greg <groove@hush.ai > wrote:[color=blue]
    > I'm trying to write a basic tool to convert strings to unicode
    > encodings. Should be easy enough, I can do the encoding bit with the
    > various encoding tools in C#, but what I can't seem to do is force C#
    > to spit out the encodings - everything I've done just seems to decode
    > the unicode and spit out exactly what I had typed in. How do I go from
    > a byte[] of unicode bytes to a string?[/color]

    Use Encoding.Unicod e.GetString(byt es);
    [color=blue]
    > (As you might guess, I'm not really much of a coder - even when I was,
    > dealing with characters was the part of coding I hated. Gimme numbers
    > any day...)[/color]

    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

    • Greg

      #3
      Re: displaying unicode encodings (ie 0x73 type notation)

      Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in message news:<MPG.1a5bb 54cd41ca0c3989c ef@msnews.micro soft.com>...[color=blue]
      > Greg <groove@hush.ai > wrote:[color=green]
      > > I'm trying to write a basic tool to convert strings to unicode
      > > encodings. Should be easy enough, I can do the encoding bit with the
      > > various encoding tools in C#, but what I can't seem to do is force C#
      > > to spit out the encodings - everything I've done just seems to decode
      > > the unicode and spit out exactly what I had typed in. How do I go from
      > > a byte[] of unicode bytes to a string?[/color]
      >
      > Use Encoding.Unicod e.GetString(byt es);[/color]

      This was what I had originally attempted, but it just decodes the
      Unicode - what I want is to write "test" into a textbox, and then get
      something like
      /0x74/0x65/0x73/0x74 in a second textbox. When I do this:

      binaryData2 = UTF8Encoding.UT F8.GetBytes(tex tBox1.Text);

      binaryData2 is loaded with exactly what I'd like to print out, but I'm
      not sure how to get it into a format that a textbox will accept. Doing
      what you suggested:

      textBox2.Text = Encoding.UTF8.G etString(binary Data2);

      Just gives me back the string "test." I suppose if there's nothing to
      do this, what I need to do is figure out how to either push bytes into
      a textbox, or how to convert bytes to chars.
      [color=blue]
      > See http://www.pobox.com/~skeet/csharp/unicode.html for more
      > information.[/color]

      Thanks for that - I'll have a go.

      Greg

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: displaying unicode encodings (ie 0x73 type notation)

        Greg <groove@hush.ai > wrote:[color=blue][color=green]
        > > Use Encoding.Unicod e.GetString(byt es);[/color]
        >
        > This was what I had originally attempted, but it just decodes the
        > Unicode - what I want is to write "test" into a textbox, and then get
        > something like
        > /0x74/0x65/0x73/0x74 in a second textbox. When I do this:
        >
        > binaryData2 = UTF8Encoding.UT F8.GetBytes(tex tBox1.Text);[/color]

        In that case what you're *really* asking is how to convert the byte
        array {0x74, 0x65, 0x73, 0x74} into the string
        "/0x74/0x65/0x73/0x74", which fortunately has nothing to do with the
        nasty business of actual decoders.

        Something like this will do it though:

        StringBuilder builder = new StringBuilder() ;
        foreach (byte b in myByteArray)
        {
        builder.AppendF ormat ("/0x{0:x2}", b);
        }
        string coded = builder.ToStrin g();

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

        • Greg

          #5
          Re: displaying unicode encodings (ie 0x73 type notation)

          Jon Skeet [C# MVP] <skeet@pobox.co m> wrote in message news:<MPG.1a5ca b5ab60fa758989c f5@msnews.micro soft.com>...[color=blue]
          > In that case what you're *really* asking is how to convert the byte
          > array {0x74, 0x65, 0x73, 0x74} into the string
          > "/0x74/0x65/0x73/0x74", which fortunately has nothing to do with the
          > nasty business of actual decoders.[/color]

          Yeah, that seems prettty accurate. Shame I didn't ask that first, eh? :)
          [color=blue]
          > Something like this will do it though:
          >
          > StringBuilder builder = new StringBuilder() ;
          > foreach (byte b in myByteArray)
          > {
          > builder.AppendF ormat ("/0x{0:x2}", b);
          > }
          > string coded = builder.ToStrin g();[/color]

          Excellent stuff - many thanks for that Jon!

          --
          Greg

          Comment

          Working...