Advertisement
Guest User

Untitled

a guest
Jan 24th, 2011
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. $Fmt::Hex = "0123456789ABCDEF";
  2. $Fmt::Dec = "0123456789";
  3. $Fmt::Oct = "01234567";
  4. $Fmt::Bin = "01";
  5.  
  6. function mDecToFmt(%a,%b)
  7. {
  8.     %c = "";
  9.     %d = strLen(%b);
  10.    
  11.     if(%a < 0)
  12.     {
  13.         %a = -%a;
  14.         %e = 1;
  15.     }
  16.     else
  17.         %e = 0;
  18.    
  19.     if(%d <= 1)
  20.         return "";
  21.    
  22.     while(1)
  23.     {
  24.         %c = getSubStr(%b,%a % %d,1) @ %c;
  25.         %a = mFloor(%a / %d);
  26.        
  27.         if(%a < 1)
  28.             break;
  29.     }
  30.    
  31.     if(%e)
  32.         %c = "-" @ %c;
  33.    
  34.     return %c;
  35. }
  36.  
  37. function mFmtToDec(%c,%b)
  38. {
  39.     %a = 0;
  40.     %d = strLen(%b);
  41.    
  42.     if(getSubStr(%c,0,1) $= "-")
  43.     {
  44.         %c = getSubStr(%c,1,strLen(%c) - 1);
  45.         %e = 1;
  46.     }
  47.     else
  48.         %e = 0;
  49.    
  50.     if(%d <= 1)
  51.         return "";
  52.    
  53.     for(%i = 0; %i < strLen(%c); %i++)
  54.     {
  55.         %f = getSubStr(%c,strLen(%c) - 1 - %i,1);
  56.        
  57.         if(strPos(%b,%f) == -1)
  58.             return "";
  59.        
  60.         %a = %a + strPos(%b,%f) * mPow(%d,%i);
  61.     }
  62.    
  63.     if(%e)
  64.         %a = -%a;
  65.    
  66.     return %a;
  67. }
  68.  
  69. function mFmtToFmt(%c,%b,%b2)
  70. {
  71.     %a = mFmtToDec(%c,%b);
  72.     return mDecToFmt(%a,%b);
  73. }
  74.  
  75. function mDecToHex(%a)
  76. {
  77.     return mDecToFmt(%a,$Fmt::Hex);
  78. }
  79.  
  80. function mHexToDec(%h)
  81. {
  82.     return mFmtToDec(%h,$Fmt::Hex);
  83. }
  84.  
  85. function mDecToOct(%a)
  86. {
  87.     return mDecToFmt(%a,$Fmt::Oct);
  88. }
  89.  
  90. function mOctToDec(%i)
  91. {
  92.     return mFmtToDec(%i,$Fmt::Oct);
  93. }
  94.  
  95. function mDecToBin(%a)
  96. {
  97.     return mDecToFmt(%a,$Fmt::Bin);
  98. }
  99.  
  100. function mBinToDec(%j)
  101. {
  102.     return mFmtToDec(%j,$Fmt::Bin);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement