Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Somebody help me to convert 8 digits(e.g.12345678) to 4 alphabet character follows with 4 digits (e.g "abcd1234").

Thanks In Advance
Posted
Comments
   
Not clear. What do you mean by "convert"? What's the rule for this calculation? And why can't you do it by yourself?
--SA
shek124 4-Jan-12 3:44am
   
Give more explaination
Amir Mahfoozi 4-Jan-12 4:17am
   
Please clarify.

1 solution

Try:
C#
string s = "12345678";
string toAlpha = s.Substring(0, 4);
string toDigit = s.Substring(4);
StringBuilder sb = new StringBuilder(8);
foreach (char c in toAlpha)
    {
    sb.Append((char)('A' + (c - '0')));
    }
sb.Append(toDigit);
string output = sb.ToString();



"while gradually increasing from "0000" to "AAAA", "0001" to "AAAB", ... "9999" to "AOUP". How will I know while decoding "AOUP" to "9999". Is any logic to know "AOUP" equal to "9999"."

Just reverse the process:


C#
private string Encode(string numeric)
    {
    string alphaPart = numeric.Substring(0, 4);
    string numPart = numeric.Substring(4);
    uint u = uint.Parse(alphaPart);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 4; i++)
        {
        sb.Insert(0, (char)('A' + (u % 26)));
        u /= 26;
        }
    sb.Append(numPart);
    return sb.ToString();
    }
private string Decode(string mixed)
    {
    string numPart = mixed.Substring(4);
    string alphaPart = mixed.Substring(0, 4);
    uint u = 0;
    foreach (char c in alphaPart)
        {
        u *= 26;
        u += (uint)(c - 'A');
        }
    return u.ToString("0000") + numPart;
    }



Please excuse the indentation - there is a problem with the auto formatter today...
   
v2
Comments
Rajee Maharjan1 4-Jan-12 4:20am
   
Thanks For Reply,
Actually, I need to encode 8 digit (e.g "12341234") to 4 alphabet with follows 4 digits(eg:"abcd1234") and also need to decode (eg: "abcd1234") to 8 digits ( e.g:"12341234").
OriginalGriff 4-Jan-12 4:25am
   
Then you need to provide rules for the encoding.
Rajee Maharjan1 4-Jan-12 4:35am
   
I had tried alot. If you have any idea please suggest me.
OriginalGriff 4-Jan-12 4:47am
   
No, what I mean is: we can't tell you what code to write, if we don't know how you want an eight digit number translated into a four letter alphabetic, and where the remaining four digits are coming from. Are they supposed to be unique? If so, we need to know where you are storing these, using these. We can't just invert a complete system for you - it may not fit what you are trying to do!
Rajee Maharjan1 4-Jan-12 4:57am
   
Actually, 8 digit can come from my table. I need to encode into (A to Z 26 character and 0 to 9 digit) 4 alphabet character with not case sensitive follow with 4 digits to make user readable easily and error less from typographic error. And I also need to decode to back write in table. I had tried one, but it was case sensitive. If any rules to encode please help me.
OriginalGriff 4-Jan-12 5:30am
   
If that is all you want to do, (and I must have missed something, because it doesn't seem to improve anything for the user) then the code I gave will do that - to reverse it is simple, provided you use ToUpper on the input string before translating the first four characters.
If you convert four digits to alpha you will either get a direct translation ( i.e a '0' becomes an 'A', a '1' becomes a 'B' ands so forth) or a numeric translation (i.e. "0000" becomes "AAAA", "0001" becomes "AAAB", ..., "9999" becomes "AOUP") and tack the last four digits on the end. Since you start with 8 chars and end with 8 chars I can't see any improvement!
Rajee Maharjan1 4-Jan-12 6:01am
   
while gradually increasing from "0000" to "AAAA", "0001" to "AAAB", ... "9999" to "AOUP". How will I know while decoding "AOUP" to "9999". Is any logic to know "AOUP" equal to "9999".
OriginalGriff 4-Jan-12 6:18am
   
Answer updated
Rajee Maharjan1 4-Jan-12 6:33am
   
Thanks, It will be helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900