int array to string

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

    #1

    int array to string

    Hello all,
    I'm feeling really stupid right now, because I can't work this out. I've
    taken a string and converted it to an array of ints, containing the unicode
    values of each character. I do some stuff with the numbers, and now I want
    to change the array back to a string. Can't figure out how. UnicodeEncoding
    class can convert bytes, but how do I deal with an array of ints?

    TIA,
    Eric


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: int array to string

    Eric,

    Instead of converting it to an array of integers, why not convert it to
    an array of characters (char)? The char type is a unicode character, so it
    can contain the value easily. Once you have that, you modify it and then
    pass the array back to the string constructor to get your string back.

    Also, if you are performing a lot of operations, you might want to store
    the character values in a StringBuilder instance.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Eric Eggermann >" <<none> wrote in message
    news:%23nUjZ%23 NuDHA.2432@TK2M SFTNGP10.phx.gb l...[color=blue]
    > Hello all,
    > I'm feeling really stupid right now, because I can't work this out.[/color]
    I've[color=blue]
    > taken a string and converted it to an array of ints, containing the[/color]
    unicode[color=blue]
    > values of each character. I do some stuff with the numbers, and now I want
    > to change the array back to a string. Can't figure out how.[/color]
    UnicodeEncoding[color=blue]
    > class can convert bytes, but how do I deal with an array of ints?
    >
    > TIA,
    > Eric
    >
    >[/color]


    Comment

    • Eric Eggermann

      #3
      Re: int array to string

      Sorry, I posted too soon. I got it. Maybe it's not the best way, but, I
      created a new array of the same size as the int array, looped through the
      ints, and used Convert.ToChar with a string builder.
      char[] chars = new char[codes.GetUpperB ound(0)];
      StringBuilder sb = new StringBuilder() ;
      for(int j = codes.GetLowerB ound(0); j <= codes.GetUpperB ound(0); j++)
      {
      sb.Append(Conve rt.ToChar(codes[j]));
      }

      Eric


      Comment

      • Eric Eggermann

        #4
        Re: int array to string


        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:uu8ntQOuDH A.2304@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Eric,
        >
        > Instead of converting it to an array of integers, why not convert it[/color]
        to[color=blue]
        > an array of characters (char)? The char type is a unicode character, so[/color]
        it[color=blue]
        > can contain the value easily. Once you have that, you modify it and then
        > pass the array back to the string constructor to get your string back.
        >
        > Also, if you are performing a lot of operations, you might want to[/color]
        store[color=blue]
        > the character values in a StringBuilder instance.
        >
        > Hope this helps.
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m[/color]

        The only reason I didn't use char is that I'm performing an addition
        operation with the char.
        Basically, I'm generating incrementing a string like the column numbering
        system in Excel. A,B,C...,Z,AA,A B,AC...,AZ,AAA. ...

        This may not be the best way, but it now works.

        /// <summary>
        /// Take a string generated by this sub, and figure the next logical
        /// item in the series.
        /// </summary>
        /// <param name="current"> The string we are 'incrementing' </param>
        /// <returns>The next string in the series. Ex: A returns B, CC returns
        CD, ZZ returns AAA</returns>
        public static string BuildNextAlphab eticCode(string current)
        {
        //Convert argument to array of ints
        int[] codes = General.StringH elp.UnicodeValu es(current);
        bool done = false;

        for(int i = codes.GetUpperB ound(0); i >= codes.GetLowerB ound(0); i--)
        {
        int incremented = codes[i] + 1;
        if(incremented <= MAX_CHAR_CODE)
        {
        codes[i] = incremented;
        done = true;
        break;
        }
        else
        {
        //set this one back to min char code
        codes[i] = CAP_A_CHAR_CODE ;
        }
        }
        //convert back to string
        char[] chars = new char[codes.GetUpperB ound(0)];
        StringBuilder sb = new StringBuilder() ;
        for(int j = codes.GetLowerB ound(0); j <= codes.GetUpperB ound(0); j++)
        {
        sb.Append(Conve rt.ToChar(codes[j]));
        }
        if(!done)
        {
        //The string needs to be one character longer.
        //add a character
        //CAP_A_CHAR_CODE ;
        sb.Append(Conve rt.ToChar(CAPA_ CHAR_CODE));
        }
        return sb.ToString();
        }

        Is there a much better way?

        Thanks for the reply,
        Eric


        Comment

        • Frank Oquendo

          #5
          Re: int array to string

          > char[] chars = new char[codes.GetUpperB ound(0)];[color=blue]
          > StringBuilder sb = new StringBuilder() ;
          > for(int j = codes.GetLowerB ound(0); j <= codes.GetUpperB ound(0);
          > j++) {
          > sb.Append(Conve rt.ToChar(codes[j]));
          > }[/color]

          So what's the char array for? Also, there's already a conversion
          operator defined for casting int to char:

          foreach (int code in codes) {
          sb.Append((char )code);
          }

          --
          There are 10 kinds of people. Those who understand binary and those who
          don't.

          http://code.acadx.com
          (Pull the pin to reply)


          Comment

          • BMermuys

            #6
            Re: int array to string

            Hi,

            <snip>
            [color=blue]
            > The only reason I didn't use char is that I'm performing an addition
            > operation with the char.[/color]

            The increment operator (++) works on a char. And you can compare char with
            other chars.

            The following code is like yours except for the conversion:

            public const char MIN_CHAR='A';
            public const char MAX_CHAR='Z';

            public static string BuildNextAlphab eticCode(string current)
            {
            StringBuilder ret = new StringBuilder(c urrent);
            bool CA = true; // cary one
            int digit = ret.Length-1;

            while ( (CA) && (digit>=0) )
            {
            char c = start[digit];
            if ( ++c > MAX_CHAR )
            {
            ret[digit] = MIN_CHAR;
            CA = true;
            }
            else
            {
            ret[digit] = c;
            CA = false;
            }
            --digit;
            }

            // handle possible overflow
            if (CA) ret.Insert(0,MI N_CHAR);

            return ret.ToString();
            }

            HTH,
            Greetings

            [color=blue]
            > Basically, I'm generating incrementing a string like the column numbering
            > system in Excel. A,B,C...,Z,AA,A B,AC...,AZ,AAA. ...
            >
            > This may not be the best way, but it now works.
            >
            > /// <summary>
            > /// Take a string generated by this sub, and figure the next logical
            > /// item in the series.
            > /// </summary>
            > /// <param name="current"> The string we are 'incrementing' </param>
            > /// <returns>The next string in the series. Ex: A returns B, CC returns
            > CD, ZZ returns AAA</returns>
            > public static string BuildNextAlphab eticCode(string current)
            > {
            > //Convert argument to array of ints
            > int[] codes = General.StringH elp.UnicodeValu es(current);
            > bool done = false;
            >
            > for(int i = codes.GetUpperB ound(0); i >= codes.GetLowerB ound(0); i--)
            > {
            > int incremented = codes[i] + 1;
            > if(incremented <= MAX_CHAR_CODE)
            > {
            > codes[i] = incremented;
            > done = true;
            > break;
            > }
            > else
            > {
            > //set this one back to min char code
            > codes[i] = CAP_A_CHAR_CODE ;
            > }
            > }
            > //convert back to string
            > char[] chars = new char[codes.GetUpperB ound(0)];
            > StringBuilder sb = new StringBuilder() ;
            > for(int j = codes.GetLowerB ound(0); j <= codes.GetUpperB ound(0); j++)
            > {
            > sb.Append(Conve rt.ToChar(codes[j]));
            > }
            > if(!done)
            > {
            > //The string needs to be one character longer.
            > //add a character
            > //CAP_A_CHAR_CODE ;
            > sb.Append(Conve rt.ToChar(CAPA_ CHAR_CODE));
            > }
            > return sb.ToString();
            > }
            >
            > Is there a much better way?
            >
            > Thanks for the reply,
            > Eric
            >
            >[/color]


            Comment

            • Eric Eggermann

              #7
              Re: int array to string

              Cheers B, that's much prettier. I hadn't thought of trying to increment a
              char like that, now it seems logical though. Plus I hadn't known you could
              access a string builder like an array.

              Thanks a lot, to you and all the guys on the group. I've gotten a lot good
              advice, and learned much the last few days. I'm looking forward to returning
              the favor.

              Eric

              "BMermuys" <bmermuys_nospa m_@hotmail.com> wrote in message
              news:jGazb.5727 9$mZ5.2007110@p hobos.telenet-ops.be...[color=blue]
              > Hi,
              >
              > <snip>
              >[color=green]
              > > The only reason I didn't use char is that I'm performing an addition
              > > operation with the char.[/color]
              >
              > The increment operator (++) works on a char. And you can compare char[/color]
              with[color=blue]
              > other chars.
              >
              > The following code is like yours except for the conversion:
              >
              > public const char MIN_CHAR='A';
              > public const char MAX_CHAR='Z';
              >
              > public static string BuildNextAlphab eticCode(string current)
              > {
              > StringBuilder ret = new StringBuilder(c urrent);
              > bool CA = true; // cary one
              > int digit = ret.Length-1;
              >
              > while ( (CA) && (digit>=0) )
              > {
              > char c = start[digit];
              > if ( ++c > MAX_CHAR )
              > {
              > ret[digit] = MIN_CHAR;
              > CA = true;
              > }
              > else
              > {
              > ret[digit] = c;
              > CA = false;
              > }
              > --digit;
              > }
              >
              > // handle possible overflow
              > if (CA) ret.Insert(0,MI N_CHAR);
              >
              > return ret.ToString();
              > }[/color]


              Comment

              Working...