|
You could simplify that quite a bit, assuming you don't plan on increasing the field size later...
const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static readonly int num_chars = chars.Length;
static string NumberToCode(int number)
{
if (number < 0 || number > num_chars * num_chars)
throw new ArgumentOutOfRangeException();
return chars[number / num_chars].ToString() + chars[number % num_chars];
}
|
|
|
|
|
You only need base 32, so you can throw some letters out. In case there are some letters that you just don't like or something like that.
As a bonus, the math is easier for 32 (not that it matters in this case..)
|
|
|
|
|
In practice, I would probably throw out "O" and "I", possibly "Z" as well just to make it clearer to people reading it.
But then, I try to avoid this kind of thing anyway!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
If you convert your numeric values to base 36 you can display the values using two characters.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
|
I'd use Base-64, but that's just me.
|
|
|
|
|
Good point. I would do the same as well.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
static public string EncodeTo64(string toEncode)
{
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
Base 64 seems to be represented in 4 position format as shown below. What am I missing?
BASE 64:
From Number 0 to Base 64:MA== ~ From Base 64 MA== to Number: 0
From Number 1 to Base 64:MQ== ~ From Base 64 MQ== to Number:1
From Number 2 to Base 64:MG== ~ From Base 64 MG== to Number: 0
From Number 3 to Base 64:MW== ~ From Base 64 MW== to Number:1
From Number 4 to Base 64:NA== ~ From Base 64 NA== to Number:4
From Number 5 to Base 64:NQ== ~ From Base 64 NQ== to Number:5
From Number 6 to Base 64:NG== ~ From Base 64 NG== to Number:4
From Number 7 to Base 64:NW== ~ From Base 64 NW== to Number:5
From Number 8 to Base 64:OA== ~ From Base 64 OA== to Number:8
From Number 9 to Base 64:OQ== ~ From Base 64 OQ== to Number:9
From Number 10 to Base 64:MTA= ~ From Base 64 MTA= to Number:10
From Number 11 to Base 64:MTE= ~ From Base 64 MTE= to Number:11
From Number 12 to Base 64:MTI= ~ From Base 64 MTI= to Number:12
From Number 13 to Base 64:MTM= ~ From Base 64 MTM= to Number:13
Base 36
From Number 71 to Base36:1Z ~ From Base36 1Z to Number:71
From Number 72 to Base36:20 ~ From Base36 20 to Number:72
From Number 73 to Base36:21 ~ From Base36 21 to Number:73
From Number 74 to Base36:22 ~ From Base36 22 to Number:74
From Number 75 to Base36:23 ~ From Base36 23 to Number:75
From Number 76 to Base36:24 ~ From Base36 24 to Number:76
From Number 77 to Base36:25 ~ From Base36 25 to Number:77
From Number 78 to Base36:26 ~ From Base36 26 to Number:78
From Number 79 to Base36:27 ~ From Base36 27 to Number:79
From Number 80 to Base36:28 ~ From Base36 28 to Number:80
From Number 81 to Base36:29 ~ From Base36 29 to Number:81
From Number 82 to Base36:2A ~ From Base36 2A to Number:82
From Number 83 to Base36:2B ~ From Base36 2B to Number:83
From Number 84 to Base36:2C ~ From Base36 2C to Number:84
From Number 85 to Base36:2D ~ From Base36 2D to Number:85
From Number 86 to Base36:2E ~ From Base36 2E to Number:86
From Number 87 to Base36:2F ~ From Base36 2F to Number:87
From Number 88 to Base36:2G ~ From Base36 2G to Number:88
From Number 89 to Base36:2H ~ From Base36 2H to Number:89
From Number 90 to Base36:2I ~ From Base36 2I to Number:90
You can never try. You either do it or you don't.
|
|
|
|
|
That type of Base-64 is for encoding text, not numbers. I meant something like what was posted but with sixty-four digits. Rather than arguing about whether to use Base-32 or Base-36, I'd just use Base-64.
|
|
|
|
|
In my world 4-digit numbers often go all the way to 9999, and then most answers aren't solving the problem.
|
|
|
|
|
I guess his world is a lot smaller.
BDF
I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be.
-- BillWoodruff
|
|
|
|
|
Yeah, something tells me it's the size of a class room. If that's true, I hope his prof finds out the code he turned in is not his own work.
|
|
|
|
|
Hehe.
He did specify up to 1000 though.
You'd need 100 unique characters to get 10,000 unique values into 2 characters. That's easily possible within the 224 available ANSI one-byte codepoints, though sadly not quite within the 96 7-bit ones.
|
|
|
|
|
sfdsfs
modified 9-Oct-12 11:01am.
|
|
|
|
|
Nice code dump
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
no formatting, no question; what is this?
|
|
|
|
|
|
Collin Jasnoch wrote: its "a module"
I see. That is exactly what I needed, I'll be borrowing it.
|
|
|
|
|
Luc Pattyn wrote: exactly what I needed
Probably a lot easier to use now...
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
If I were you, I would ditch the commented out lines. I would also look into using string.IsNullOrEmpty (or string.IsNullOrWhitespace if you are using .NET 4).
As you've removed your code, here it is for anyone who wonders what my comments are about:
using System;
using System.Web;
using System.Xml;
public class SecurityHttpModule : IHttpModule
{
public SecurityHttpModule() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(Application_BeginRequest);
}
private void Application_BeginRequest(object source, EventArgs e)
{
HttpContext context = ((HttpApplication)source).Context;
string ipAddress = context.Request.UserHostAddress;
string strIPAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIPAddress == null || strIPAddress == "")
{
");
// st += " Key: " + myservab[i] + "
" ;
// string[] myservab2 = context.Request.ServerVariables.GetValues(myservab[i]);
// for (int j = 0; j < myservab2.Length; j++)
// {
// st1 += "Key : " + myservab[i] + " Value : " + myservab2[j] + "
";
// //Response.Write("Value " + myservab2 + ": " + Server.HtmlEncode(myservab2[j]) + "
");
// }
//}
strIPAddress = context.Request.ServerVariables["REMOTE_ADDR"].ToString();
}
if (!IsValidIpAddress(ipAddress))
{
context.Response.StatusCode = 403; // (Forbidden)
}
else
{
string filePath = context.Request.FilePath;
string fileExtension =
VirtualPathUtility.GetExtension(filePath);
if (fileExtension.Equals(".aspx"))
{
context.Response.Write("
" +
"HelloWorldModule: Beginning of Request" +
"
--------------------------------------------------------------------------------
");
}
}
}
private bool IsValidIpAddress(string ipAddress)
{
double myipnom = IPAddressToNumber(ipAddress);
//long myipnom = ip2ipno(ipAddress);
// long myipra = 3401007104;
string myip = ipno2ipaddress(myipnom);
bool sta = false;
Class1 obj = new Class1();
XmlDocument xmldoc = obj.getdata(myipnom);
//xmldoc.InnerXml
// XmlNodeList xmlist = xmldoc.SelectNodes("CountryIps[@*]");
XmlNodeList xmlist = xmldoc.GetElementsByTagName("CountryIps");
// XmlNodeList xmlist = xmldoc.InnerXml("NewDataSet[@*]");
//XmlNodeList xmll=xmldoc.InnerXml
foreach (XmlNode nl in xmlist)
{
if (nl != null)
{
// XmlNode tablexml = nl.SelectSingleNode("id");
string ipFrom = nl["ipFROM"].InnerXml;
string ipTo = nl["ipTO"].InnerXml;
string countrySHORT = nl["countrySHORT"].InnerXml;
string countryLONG = nl["countryLONG"].InnerXml;
bool conf;
if (ipFrom != null && ipTo != null)
{
conf = IsInRange(myipnom, double.Parse(ipFrom), double.Parse(ipTo));
if (conf == true && countrySHORT=="IN")
{
sta = true;
}
else { sta = false; }
}
}
else
{
sta = false;
}
}
return sta;
// return (ipAddress == "127.0.0.1");
}
public bool IsInRange(double ipNo, double ipFrom, double ipTo)
{
Int64 ipf= Convert.ToInt64(ipFrom);
Int64 ipn = Convert.ToInt64(ipNo);
Int64 ipt = Convert.ToInt64(ipTo);
return ipn >= ipf && ipn <= ipt;
}
public long ip2ipno(string ipAddress)
{
char[] splitchar = { '.' };
string[] strary = ipAddress.Split(splitchar);
long ipNo;
double ipno1;
double ipNumber, ipNumber1, ipNumber2, ipNumber3;
ipNumber = (Math.Pow(2, 24) * int.Parse(strary[0]));
ipNumber1 = (Math.Pow(2, 16) * int.Parse(strary[1]));
ipNumber2 = (Math.Pow(2, 8) * int.Parse(strary[2]));
ipNumber3 = int.Parse(strary[3]);
ipno1 = (ipNumber + ipNumber1+ ipNumber2 + ipNumber3);
ipNo = (Convert.ToInt64(ipNumber) + Convert.ToInt64( ipNumber1 )+ Convert.ToInt32( ipNumber2 )+ Convert.ToInt32(ipNumber3));
return ipNo;
}
public double IPAddressToNumber(string IPaddress)
{
int i;
string[] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for (i = arrDec.Length - 1; i >= 0; i = i - 1)
{
num += ((int.Parse(arrDec[i]) % 256) * Math.Pow(256, (3 - i)));
}
return num;
}
}
public string ipno2ipaddress(double ipNo)
{
double w, x, y, z;
w = (ipNo / 16777216) % 256;
x = (ipNo / 65536) % 256;
y = (ipNo / 256) % 256;
z = (ipNo) % 256;
int w1 = Convert.ToInt16(Math.Floor(w));
int x1 = Convert.ToInt16(Math.Floor(x));
int y1 = Convert.ToInt16(Math.Floor(y));
int z1 = Convert.ToInt16(Math.Floor(z));
//int w1 = Math.Floor(w);
return (w1)+ "." + Convert.ToInt32(x1) + "." + Convert.ToInt32(y1) + "." + Convert.ToInt32(z1);
}
public void Dispose() { /* clean up */ }
}
//XmlDocument xml = new XmlDocument();
//xml.LoadXml(myXmlString); //myXmlString is the xml file in string //copying xml to string: string myXmlString = xmldoc.OuterXml.ToString();
//XmlNodeList xnList = xml.SelectNodes("/Element[@*]/ANode/BNode/CNode");
//foreach (XmlNode xn in xnList)
//{
// XmlNode example = xn.SelectSingleNode("Example");
// if (example != null)
// {
// string na = example["Name"].InnerText;
// string no = example["NO"].InnerText;
// }
//}
|
|
|
|
|
Hi,all
I am using dataset and and placing two tables on dataset.xsd pane from my database. so it is valid or i have to manually create tables by right clicking on dataset.xsd pane and select new table.
And if it is valid,then i am fetching data from database of that two tables. So what should i write in adapter.fill(i.e. ds,"which table name");
beacause i am getting my report blank.even if there is data in dataset.
and if i create only one datatable with the columns from two tables in the dataset..i am only getting first row on the report..but actually there are 3 rows..so i m not getting the problem
|
|
|
|
|
Hello guys. I have a Tab Control in which I have placed two Form s. The advantage of this approach is: I designed the Form separately and it has it's own class (unlike TabPage). TabPage is the parent and the Windows Form is the child control here.
But I am having a small problem. When I run the application, the GUI of the the forms is little disturbed. I mean the controls were not there, where I placed them.
NOTE: I also ran the application by modifying form's size w.r.t tab's size but it did not help much: like this
Form1.Height = Tab1.Height;
Form1.Width = Tab1.Width;
If don't change form's original size and show it in messagebox, then this does not show original size which I set at the design time. What could I do to tackle this issue? Thanks for any pointers.
This world is going to explode due to international politics, SOON.
modified 16-Jul-12 5:20am.
|
|
|
|
|
First of all, you can also use a UserControl instead of placing forms in tab pages. Also have a look at the Anchor property[^]
|
|
|
|
|
Obaid ur Rehman wrote: you can also use a UserControl instead of placing forms
Magar yeh meri application requirement nhi hai. Agar main aisa karta to mujhay UserControl ka code bhi apnay customer ko dena parhay ga, jo keh main nhi chahta. Waisay bhi, kitna ajeeb lagay gaa jab hum pooray ka poora inventory form design kar kay, ussay UserControl bnaa dain.
I have seen people preferring my approach over the one you suggested. I think it depends mostly on application requirement.
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Hi, all
There is a WebBrowser in my program. The user select a HtmlElement in the WebBrowser of my program, by a mouse click. So, the program can use the function GetElementFromPoint to detecte which HtmlElement is selected:
HtmlElement htmlEM=GetElementFromPoint(...);
As a result, the program can get all the information using the htmlEM if needed. Then the program writes some information on the hard-disk, and quit. At last, the user runs the program again. and the program reads the information on the hard-disk, and the WebBrowser navigates to the Website again, and automatically gets back the HtmlElement selected just now.
The question is : what information should be write down for the program to get the HtmlElement back?
Since the htmlEM is selected by the user, it could be any HtmlElement, may be a <div>,<p> or even an <iframe>. We don't sure it have an id or not, Also, we don't know its InnerText, it may be the current time, and changes with the time. Even worse, it may be a floating <div>, containing an note or a AD, and keeps moving all the time.
I think the URL of the website will be needed, since the software needs it to navitate to the website again.
However, I don't what to do next. Help is needed.
The question is : what information should be write down for the software to get the HtmlElement back?
modified 16-Jul-12 4:09am.
|
|
|
|