Jump to content
  • Advertisement
Sign in to follow this  
GreenGiant

[.net] C# (ASP.net) char encoding

This topic is 4291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to encode a string in a project of mine, to return the numerical value of each letter. So: ABC = 1-2-3 Moo: 13-15-15 Only reason I need it hyphenated is I don't have a clue how it would be able to decode it afterwards without something to seperate each letter. I have Googled this, and all I can find is:
int encode(char ch)
{
    return (int)Encoding.ASCII.GetBytes(ch)[0];
}

But so far this isn't working. Can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
Something like this?


// Input string:
string input = "ABC";

// The stringbuilder is 4 times as long (should be able to accomodate the entire string).
StringBuilder SB = new StringBuilder(input.Length * 4);

// Cycle through each character:
for (int i = 0; i < input.Length; ++i) {
if (i != 0) SB.Append("-");
SB.Append(((int)input).ToString()); // You might not need the ToString() bit
}

// That's it...
return SB.ToString();


[Edited by - benryves on October 18, 2006 9:57:27 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
Something like this?


// Input string:
string input = "ABC";

// The stringbuilder is 4 times as long (should be able to accomodate the entire string).
StringBuilder SB = new StringBuilder(input.Length * 4);

// Cycle through each character:
foreach (int i = 0; i < input.Length; ++i) {
if (i != 0) SB.Append("-");
SB.Append(((int)input).ToString()); // You might not need the ToString() bit
}

// That's it...
return SB.ToString();


Hi, thanks for the reply. I'm assuming you meant
for
, and not
foreach
.

It seems to be the right idea, but I can't really understand why it returns 97-98-99 when I enter "ABC". Shouldn't A=1, B=2, C=3?

Cheers.

Share this post


Link to post
Share on other sites
Whoops, sorry, I originally used foreach (char c in input) but it makes life easier (with the hyphens) to use a for loop (and didn't fix all of the code).

Casting a char to an integral type returns the Unicode value used to represent the character internally - and the basic Latin alphabet is represented using the same character codes as ASCII.

You could try forcing the original string to uppercase (.ToUpper()) and using:

(int)((int)input - (int)'A' + 1)

// instead of
(int)input


Note that you should ensure that only valid characters are used, else you'll end up with negative values (which will confuse the hypenation).

Share this post


Link to post
Share on other sites
Well after a couple of hours of toying with this, I'm still quite stuck. The annoying thing it's almost there. I was reading this PDF earlier, and saw that 0033 in Unicode is '3', which is exactly what the following code outputs when you put 'C'.


protected string PerformEncoding(string input)
{
input.ToUpper();
// The stringbuilder is 4 times as long (should be able to accomodate the entire string).
StringBuilder SB = new StringBuilder(input.Length);

// Cycle through each character:
for (int i = 0; i < input.Length; i++)
{
if (i != 0) SB.Append("-");
SB.Append((int)input - (int)'A' + 1).ToString(); // You might not need the ToString() bit
}

// That's it...
return SB.ToString();
}



What I just can't figure out is how to convert 0033 to '3'. Any ideas?

Share this post


Link to post
Share on other sites
The problem is in the cast on this ine of code:
SB.Append(((int)input).ToString()); Original
SB.Append(((char)input).ToString()); Corrected, now it will return A-B-C

// Input string:
string input = "ABC";

// The stringbuilder is 4 times as long (should be able to accomodate the entire string).
StringBuilder SB = new StringBuilder(input.Length * 4);

// Cycle through each character:
for (int i = 0; i < input.Length; ++i)
{
if (i != 0) SB.Append("-");
SB.Append(((char)input).ToString()); // You might not need the ToString() bit
}

// That's it...
return SB.ToString();

Share this post


Link to post
Share on other sites
The problem is the cast on this line:
SB.Append(((int)input).ToString()); Original
SB.Append(((char)input).ToString());Corrected, it will return A-B-C

// Input string:
string input = "ABC";

// The stringbuilder is 4 times as long (should be able to accomodate the entire string).
StringBuilder SB = new StringBuilder(input.Length * 4);

// Cycle through each character:
for (int i = 0; i < input.Length; ++i)
{
if (i != 0) SB.Append("-");
SB.Append(((char)input).ToString()); // You might not need the ToString() bit
}

// That's it...
return SB.ToString();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!