Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleStrings
- {
- class Stringklasse
- {
- private string str1;
- public Stringklasse(string str1)
- {
- this.str1 = str1;
- }
- public bool IsPalindroom()
- {
- bool palindroom = true;
- for (int i = 1; i < str1.Length / 2; i++)
- {
- if (str1[i] != str1[str1.Length - i])
- palindroom = false;
- }
- return palindroom;
- }
- public bool IsBinair()
- {
- bool binair = true;
- for (int i = 1; i < str1.Length; i++)
- {
- if (str1[i] != '1' && str1[i] != '0')
- binair = false;
- }
- return binair;
- }
- public bool BinToDec()
- {
- int decimaal=0;
- int gewicht = 1;
- if (IsBinair() == false)
- return false;
- for (int i = str1.Length-1; i >=0; i--)
- {
- decimaal += ((str1[i])-48) * gewicht;
- gewicht *= 2;
- }
- Console.WriteLine(decimaal);
- return true;
- }
- public bool IsHexadec()
- {
- bool hexadec = true;
- str1=str1.ToUpper();
- for (int i=1;i<str1.Length;i++)
- {
- char c = str1[i];
- hexadec = ((c >= '0' && c <= '9') ||
- (c >= 'A' && c <= 'F'));
- if(!hexadec)
- return false;
- }
- return hexadec;
- //return System.Text.RegularExpressions.Regex.IsMatch(str1, @"\A\b[0-9a-fA-F]+\b\Z");
- }
- public bool HexaToDec()
- {
- int decimaal = 0;
- int gewicht = 1;
- int waarde = 0;
- if (!IsHexadec())
- return false;
- for ( int i = str1.Length - 1;i>=0;i--)
- {
- if (str1[i] >= '0' && str1[i] <= '9')
- waarde = str1[i] - 48;
- else if (str1[i] >= 'A' && str1[i] <= 'F')
- waarde = str1[i] - 55;
- decimaal = waarde * gewicht;
- gewicht *= 16;
- }
- Console.WriteLine(decimaal);
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement