conversion to c# from excel function help

D

D

I am trying to do this in C#

Function Num2Let(L As Long) As String
Dim s0 As String, s1 As String, S2 As String, s3 As String
If L > 18278 Then s0 = Chr((Int((L - 18279) / 17576) Mod 26) + 65)
If L > 702 Then s1 = Chr((Int((L - 703) / 676) Mod 26) + 65)
If L > 26 Then S2 = Chr(Num2Let & (Int((L - 27) / 26)) Mod 26 + 65)
s3 = Chr(((L - 1) Mod 26) + 65)
Num2Let = s0 & s1 & S2 & s3
End Function

it takes a long and converts it to the excel header format of AA, BQ DS etc

so far I have this, I commented out the top and worked my way up from the
bottom. I'm not sure what the c# equivalent of Int in excel (which returns
the 210 part of 210.2342) and I don't know what how this is handled
Chr(Num2Let & ( Int((L - 27) / 26) ) % 26 + 65);

Thanks for any help.


public string Num2Let(long L )

{

string s0, s1, S2, s3, final;

/* if(L > 18278)

s0 = Chr((Int((L - 18279) / 17576) % 26) + 65);

if(L > 702)

s1 = Chr((Int((L - 703) / 676) % 26) + 65);

*/

if(L > 26)

S2 = Chr(Num2Let(((L - 27) / 26)) % 26 + 65);

//S2 = Chr(Num2Let & ( Int((L - 27) / 26) ) % 26 + 65);

long x =(((L - 1) % 26) + 65);

char c = (char)x;

s3 = c.ToString();

//final = s0 + s1 + S2 + s3;

return s3;

}
 
R

RCS

Man, you should make up a book of these and sell them at geek stores, you'd
make a fortune!!

If this doesn't get solved tonight, I'll give it a shot tomorrow - looks
fun!!
 
D

D

I wish I could but I didn't write it. I found it when I was looking for a
function to convert numbers to excel like headers (AAA, AAB, AAC etc).

I'll post the solution if I figure it out.
 
D

Dennis Myrén

Try this one.
I needed one myself too. Cool!

Do not know if it works yet.

public static string CellIndexToName ( int index )
{
if (0x0 >= index)
return string.Empty;
StringBuilder sb = new StringBuilder(0x5);
if (0x4766 < index)
{
sb.Append((char) ((int) ((int) ((double) (((double) (index - 0x4767)) /
17576)) % 0x1a) + 0x41));
}
if (0x2BE < index)
{
sb.Append((char) ((int) ((int) ((double) (((double) (index - 0x2bf)) /
676)) % 0x1a) + 0x41));
}
if (0x1A < index)
{
sb.Append((char) ((int) ((int) ((double) (((double) (index - 0x1b)) /
26)) % 0x1a) + 0x41));
}
sb.Append((char) (int) (((index - 0x1) % 0x1A) + 0x41));
return sb.ToString();
}
 
D

D

BRILLIANT!!!!!

Works fine for me so far.

Can you tell me why you used hex (0x0 , 0x4766, etc)?

Thanks alot!!!
 
D

Dennis Myrén

So it works? Cool!
Have'nt really got the time to test it properly, just a few times with small
numbers.

There is no particular reason why i use hex instead of decimals, it is just
a matter
of preference, and maybe decimals would be clearer when posting code here.
 
D

D

I haven't pushed it to the higher limits but so far it works good.
There is no particular reason why i use hex instead of decimals, it is
just

It looks cooler!

I worked with a guy who always wrote his if's in a similiar fashion although
not hex but put the number first like
this
if ( 1 < x )
versus
if ( x > 1 )


he said the reason was that it was a faster action in the cpu. I don't know
about that but it "sounded good to management."

Thanks again.
 

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