int array to string

E

Eric Eggermann

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
 
N

Nicholas Paldino [.NET/C# MVP]

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

Eric Eggermann

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
 
E

Eric Eggermann

Nicholas Paldino said:
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.

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 + 1;
if(incremented <= MAX_CHAR_CODE)
{
codes = incremented;
done = true;
break;
}
else
{
//set this one back to min char code
codes = 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
 
F

Frank Oquendo

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]));
}

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

BMermuys

Hi,

The only reason I didn't use char is that I'm performing an addition
operation with the char.

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

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 + 1;
if(incremented <= MAX_CHAR_CODE)
{
codes = incremented;
done = true;
break;
}
else
{
//set this one back to min char code
codes = 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
 
E

Eric Eggermann

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 said:
Hi,

The only reason I didn't use char is that I'm performing an addition
operation with the char.

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top