Dave

Dave

  • NA
  • 2
  • 0

Converting integers to bits/bytes

Feb 26 2008 3:46 AM
Hello there. I am not a sessoned programmer and actually have very little experiance in c#. I have undertaken a project and one of the functions of my porject is to turn string numbers, into bits or bit vectors I beleive they are called. Like I said I am not a brilliant programmer and have had much help with this before. I have currently been changing the strings to ints first. But now I realised that bit vectors are more relevant. The code I am using is: private void button4_Click(object sender,System.EventArgs e) { string text =txtBoxBefore.Text; string expression =@"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"; text =Regex.Replace(text, expression, new MatchEvaluator(IpToUInt)); txtBoxAfter.Text=(text); } public static string IpToUInt (Match m) { string[] temp =m.Value.Split('.'); uint[] octets = new uint[4]; for (int i = 0; i < 4; i++) { octets[i] =uint.Parse(temp[i]); } return (octets[0] +(octets[1] << 8) + (octets[2] << 16) +(octets[3] << 24)).ToString(); } public static string IpFromUInt2 (uint ui) { uint[] octets = new uint[4]; octets[3] = ui >> 24; uint remainder = ui -(octets[3] << 24); octets[2] = remainder >> 16; remainder -= octets[2] << 16; octets[1] = remainder >> 8; remainder -= octets[1] << 8; octets[0] = remainder; string[] temp = new string[4]; for (int i = 0; i < 4; i++) { temp[i] = octets[i].ToString(); } return String.Join(".", temp); } Any help would be great. Thanks And im also guessing that seeing that ive tried to edit this three times now it doesnt allow for spaccing between sentances.

Answers (1)