base64 encoding - characters above 127

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

    base64 encoding - characters above 127

    I seem to be having a problem base64 encoding characters above 127. I
    can encode a sentence like "The big bad dog" without problems, but if
    I try to encode something like 0xFF I get different results than in
    Perl.

    For example I am using:

    byte[] binaryArray =
    System.Text.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
    string pushString = System.Convert. ToBase64String( binaryArray);
    MessageBox.Show (pushString);

    For 0xFF I get the result: Pw== which doesn't match my results in
    Perl.

    If it helps, I am converting a HEX input from a string, like FFE0 etc
    with the following:

    for (int i=0;i<hexString .Length;i=i+2)
    {
    string tmpstr = (((char)hexStri ng[i]).ToString()+(( char)hexString
    [i+1]).ToString());
    int hex = Convert.ToInt32 (tmpstr,16);
    binaryString += (char)hex;
    }

    I run into problems with ASCII.GetBytes( )

    It seems it takes my 255 for FF and turns it into 127

    If I use Unicode instead I get 255, but it uses two spots in the byte
    array, like

    255 0

    Essentially I need FFFFFFFF as an input to turn into a byte array of:

    255 255 255 255 255 255 255 255

    Any ideas?

    Thanks,

    Curt
  • mikeb

    #2
    Re: base64 encoding - characters above 127

    Curt Fluegel wrote:[color=blue]
    > I seem to be having a problem base64 encoding characters above 127. I
    > can encode a sentence like "The big bad dog" without problems, but if
    > I try to encode something like 0xFF I get different results than in
    > Perl.
    >
    > For example I am using:
    >
    > byte[] binaryArray =
    > System.Text.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
    > string pushString = System.Convert. ToBase64String( binaryArray);
    > MessageBox.Show (pushString);
    >
    > For 0xFF I get the result: Pw== which doesn't match my results in
    > Perl.
    >
    > If it helps, I am converting a HEX input from a string, like FFE0 etc
    > with the following:
    >
    > for (int i=0;i<hexString .Length;i=i+2)
    > {
    > string tmpstr = (((char)hexStri ng[i]).ToString()+(( char)hexString
    > [i+1]).ToString());
    > int hex = Convert.ToInt32 (tmpstr,16);
    > binaryString += (char)hex;
    > }
    >
    > I run into problems with ASCII.GetBytes( )
    >
    > It seems it takes my 255 for FF and turns it into 127
    >
    > If I use Unicode instead I get 255, but it uses two spots in the byte
    > array, like
    >
    > 255 0
    >
    > Essentially I need FFFFFFFF as an input to turn into a byte array of:
    >
    > 255 255 255 255 255 255 255 255
    >
    > Any ideas?[/color]

    Instead of using the ASCII encodeing (which does, in fact, only return
    values in the range 0-127), use the encoding returned by the
    Encoding.Defaul t() static method (which returns the ANSI encoding for
    your machine setup).

    --
    mikeb

    Comment

    • Curt Fluegel

      #3
      Re: base64 encoding - characters above 127

      Ok,

      Another question, what is the best way to turn a string of hex values like:

      foo = "FFEEFF"

      into something that can be base encoded?

      My current code:
      for (int i=0;i<hexString .Length;i=i+2)

      {

      string tmpstr =
      (((char)hexStri ng[i]).ToString()+(( char)hexString[i+1]).ToString());
      int hex = Convert.ToInt16 (tmpstr,16);
      binaryString += (char)hex;

      }

      Essential the output of FFEEFF should be a string equivelent. The perl
      encoder comes up with "ÿîÿ" and I am hoping to get the same!

      Seems to barf on some characters, anything between 127 and 159 decimal to be
      exact. They all are treated as invalid and turned in decimal 63. I swear I
      am not doing anything that was supposed to be this hard :)

      Thanks,

      Curt



      "mikeb" <mailbox.google @mailnull.com> wrote in message
      news:eF%23flW3w DHA.1912@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > Curt Fluegel wrote:[color=green]
      > > I seem to be having a problem base64 encoding characters above 127. I
      > > can encode a sentence like "The big bad dog" without problems, but if
      > > I try to encode something like 0xFF I get different results than in
      > > Perl.
      > >
      > > For example I am using:
      > >
      > > byte[] binaryArray =
      > > System.Text.ASC IIEncoding.ASCI I.GetBytes(bina ryString);
      > > string pushString = System.Convert. ToBase64String( binaryArray);
      > > MessageBox.Show (pushString);
      > >
      > > For 0xFF I get the result: Pw== which doesn't match my results in
      > > Perl.
      > >
      > > If it helps, I am converting a HEX input from a string, like FFE0 etc
      > > with the following:
      > >
      > > for (int i=0;i<hexString .Length;i=i+2)
      > > {
      > > string tmpstr = (((char)hexStri ng[i]).ToString()+(( char)hexString
      > > [i+1]).ToString());
      > > int hex = Convert.ToInt32 (tmpstr,16);
      > > binaryString += (char)hex;
      > > }
      > >
      > > I run into problems with ASCII.GetBytes( )
      > >
      > > It seems it takes my 255 for FF and turns it into 127
      > >
      > > If I use Unicode instead I get 255, but it uses two spots in the byte
      > > array, like
      > >
      > > 255 0
      > >
      > > Essentially I need FFFFFFFF as an input to turn into a byte array of:
      > >
      > > 255 255 255 255 255 255 255 255
      > >
      > > Any ideas?[/color]
      >
      > Instead of using the ASCII encodeing (which does, in fact, only return
      > values in the range 0-127), use the encoding returned by the
      > Encoding.Defaul t() static method (which returns the ANSI encoding for
      > your machine setup).
      >
      > --
      > mikeb[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: base64 encoding - characters above 127

        Curt Fluegel <curt@thewebpra ctice.com> wrote:

        <snip>
        [color=blue]
        > Essential the output of FFEEFF should be a string equivelent. The perl
        > encoder comes up with "?î?" and I am hoping to get the same!
        >
        > Seems to barf on some characters, anything between 127 and 159 decimal tobe
        > exact. They all are treated as invalid and turned in decimal 63. I swear I
        > am not doing anything that was supposed to be this hard :)[/color]

        My guess is that again, you're treating the characters as ASCII when
        ASCII only has characters 0-127.

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

        • mikeb

          #5
          Re: base64 encoding - characters above 127

          Curt Fluegel wrote:
          [color=blue]
          > Ok,
          >
          > Another question, what is the best way to turn a string of hex values like:
          >
          > foo = "FFEEFF"
          >
          > into something that can be base encoded?
          >
          > My current code:
          > for (int i=0;i<hexString .Length;i=i+2)
          >
          > {
          >
          > string tmpstr =
          > (((char)hexStri ng[i]).ToString()+(( char)hexString[i+1]).ToString());
          > int hex = Convert.ToInt16 (tmpstr,16);
          > binaryString += (char)hex;
          >
          > }
          >
          > Essential the output of FFEEFF should be a string equivelent. The perl
          > encoder comes up with "ÿîÿ" and I am hoping to get the same!
          >
          > Seems to barf on some characters, anything between 127 and 159 decimal to be
          > exact. They all are treated as invalid and turned in decimal 63. I swear I
          > am not doing anything that was supposed to be this hard :)
          >[/color]

          If I understand what you want to do, you just need to convert the string
          of hex characters into a byte array, then base 64 encode that. There's
          no reason to convert your hex string into some other type of string first:

          string hexstring = "FFEEFF";

          System.Collecti ons.ArrayList temp =
          new System.Collecti ons.ArrayList() ;

          // note that the conversion being done in this loop
          // will throw one of several expressions if hexstring is
          // not well formed. I leave it up to you to add
          // error handling appropriate to your situation
          for (int i = 0; i < hexstring.Lengt h; i += 2) {
          string hexbyte = hexstring.Subst ring( i, 2);
          byte b = Convert.ToByte( hexbyte, 16);
          temp.Add( b);
          }

          byte [] convertedhex = new byte[temp.Count];
          temp.CopyTo( convertedhex);

          string base64encoded = System.Convert. ToBase64String( convertedhex);

          [color=blue]
          > Thanks,
          >
          > Curt
          >
          >
          >
          > "mikeb" <mailbox.google @mailnull.com> wrote in message
          > news:eF%23flW3w DHA.1912@TK2MSF TNGP09.phx.gbl. ..
          >[color=green]
          >>Curt Fluegel wrote:
          >>[color=darkred]
          >>>I seem to be having a problem base64 encoding characters above 127. I
          >>>can encode a sentence like "The big bad dog" without problems, but if
          >>>I try to encode something like 0xFF I get different results than in
          >>>Perl.
          >>>
          >>>For example I am using:
          >>>
          >>>byte[] binaryArray =
          >>>System.Text. ASCIIEncoding.A SCII.GetBytes(b inaryString);
          >>>string pushString = System.Convert. ToBase64String( binaryArray);
          >>>MessageBox.S how(pushString) ;
          >>>
          >>>For 0xFF I get the result: Pw== which doesn't match my results in
          >>>Perl.
          >>>
          >>>If it helps, I am converting a HEX input from a string, like FFE0 etc
          >>>with the following:
          >>>
          >>>for (int i=0;i<hexString .Length;i=i+2)
          >>>{
          >>> string tmpstr = (((char)hexStri ng[i]).ToString()+(( char)hexString
          >>> [i+1]).ToString());
          >>> int hex = Convert.ToInt32 (tmpstr,16);
          >>> binaryString += (char)hex;
          >>>}
          >>>
          >>>I run into problems with ASCII.GetBytes( )
          >>>
          >>>It seems it takes my 255 for FF and turns it into 127
          >>>
          >>>If I use Unicode instead I get 255, but it uses two spots in the byte
          >>>array, like
          >>>
          >>>255 0
          >>>
          >>>Essentiall y I need FFFFFFFF as an input to turn into a byte array of:
          >>>
          >>>255 255 255 255 255 255 255 255
          >>>
          >>>Any ideas?[/color]
          >>
          >>Instead of using the ASCII encodeing (which does, in fact, only return
          >>values in the range 0-127), use the encoding returned by the
          >>Encoding.Defa ult() static method (which returns the ANSI encoding for
          >>your machine setup).
          >>
          >>--
          >>mikeb[/color]
          >
          >
          >[/color]

          --
          mikeb

          Comment

          Working...