Connecting Tech Pros Worldwide Forums | Help | Site Map

int array to string

Eric Eggermann
Guest
 
Posts: n/a
#1: Nov 15 '05
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]
Guest
 
Posts: n/a
#2: Nov 15 '05

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

"Eric Eggermann >" <<none> wrote in message
news:%23nUjZ%23NuDHA.2432@TK2MSFTNGP10.phx.gbl...[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]


Eric Eggermann
Guest
 
Posts: n/a
#3: Nov 15 '05

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.GetUpperBound(0)];
StringBuilder sb = new StringBuilder();
for(int j = codes.GetLowerBound(0); j <= codes.GetUpperBound(0); j++)
{
sb.Append(Convert.ToChar(codes[j]));
}

Eric


Eric Eggermann
Guest
 
Posts: n/a
#4: Nov 15 '05

re: int array to string



"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote in
message news:uu8ntQOuDHA.2304@TK2MSFTNGP12.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.com[/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,AB,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 BuildNextAlphabeticCode(string current)
{
//Convert argument to array of ints
int[] codes = General.StringHelp.UnicodeValues(current);
bool done = false;

for(int i = codes.GetUpperBound(0); i >= codes.GetLowerBound(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.GetUpperBound(0)];
StringBuilder sb = new StringBuilder();
for(int j = codes.GetLowerBound(0); j <= codes.GetUpperBound(0); j++)
{
sb.Append(Convert.ToChar(codes[j]));
}
if(!done)
{
//The string needs to be one character longer.
//add a character
//CAP_A_CHAR_CODE;
sb.Append(Convert.ToChar(CAPA_CHAR_CODE));
}
return sb.ToString();
}

Is there a much better way?

Thanks for the reply,
Eric


Frank Oquendo
Guest
 
Posts: n/a
#5: Nov 15 '05

re: int array to string


> char[] chars = new char[codes.GetUpperBound(0)];[color=blue]
> StringBuilder sb = new StringBuilder();
> for(int j = codes.GetLowerBound(0); j <= codes.GetUpperBound(0);
> j++) {
> sb.Append(Convert.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)


BMermuys
Guest
 
Posts: n/a
#6: Nov 15 '05

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 BuildNextAlphabeticCode(string current)
{
StringBuilder ret = new StringBuilder(current);
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,MIN_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,AB,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 BuildNextAlphabeticCode(string current)
> {
> //Convert argument to array of ints
> int[] codes = General.StringHelp.UnicodeValues(current);
> bool done = false;
>
> for(int i = codes.GetUpperBound(0); i >= codes.GetLowerBound(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.GetUpperBound(0)];
> StringBuilder sb = new StringBuilder();
> for(int j = codes.GetLowerBound(0); j <= codes.GetUpperBound(0); j++)
> {
> sb.Append(Convert.ToChar(codes[j]));
> }
> if(!done)
> {
> //The string needs to be one character longer.
> //add a character
> //CAP_A_CHAR_CODE;
> sb.Append(Convert.ToChar(CAPA_CHAR_CODE));
> }
> return sb.ToString();
> }
>
> Is there a much better way?
>
> Thanks for the reply,
> Eric
>
>[/color]


Eric Eggermann
Guest
 
Posts: n/a
#7: Nov 15 '05

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_nospam_@hotmail.com> wrote in message
news:jGazb.57279$mZ5.2007110@phobos.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 BuildNextAlphabeticCode(string current)
> {
> StringBuilder ret = new StringBuilder(current);
> 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,MIN_CHAR);
>
> return ret.ToString();
> }[/color]


Closed Thread


Similar C# / C Sharp bytes