Tag: convert

C#

Convert Subnet to CIDR in C#

int Mask2CIDR(IPAddress subnetmask) { try { Byte[] ipbytes = subnetmask.GetAddressBytes();   uint subnet = 16777216 * Convert.ToUInt32(ipbytes[0]) + 65536 * Convert.ToUInt32(ipbytes[1]) + 256 * Convert.ToUInt32(ipbytes[2]) + Convert.ToUInt32(ipbytes[3]); uint mask = 0x80000000; uint subnetConsecutiveOnes = 0;   for (int i = 0; i < 32; i++) { if (!(mask & subnet).Equals(mask)) break;   subnetConsecutiveOnes++; mask = …

General

Convert String to byte array in C#

Converting a byte array to string (unicode): byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("unicode characters"); String s = Encoding.Unicode.GetString(b); Converting a byte array to string (ascii): byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);byte[] b = Encoding.Unicode.GetBytes("ascii characters"); String s = Encoding.Unicode.GetString(b);

C#

Convert byte array to String in C#

Assuming that byte array contains data in Unicode format: byte[] data = ….; String data_s = Encoding.Unicode.GetString(data);byte[] data = ….; String data_s = Encoding.Unicode.GetString(data); Assuming that byte array contains data in ASCII format: byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);byte[] data = ….; String data_s = Encoding.ASCII.GetString(data);

C#

Convert RTF characters to Greek characters

Let’s see how we can convert RTF(Rich Text Format) characters (RichTextbox C#) to Greek characters. public String Rtf2Greek(String s) { s = s.Replace("’c1", "Α"); s = s.Replace("’c2", "Β"); s = s.Replace("’c3", "Γ"); s = s.Replace("’c4", "Δ"); s = s.Replace("’c5", "Ε"); s = s.Replace("’c6", "Ζ"); s = s.Replace("’c7", "Η"); s = s.Replace("’c8", "Θ"); s = s.Replace("’c9", …