title: How to fix "Value was either too large or too small for a UInt32" C#
body: <p>I am trying to convert ActionScript3 code to C# that's like the main thing. However, with trying to convert one of the functions I got the error which is in the title when I was trying to convert a hexadecimal string to an int.</p>
  
  <p>Basically, this code is supposed to take information for example user data and then do somethings and in the end return Base64 encoded text. The main error that I am aware of is at the part where "loc9 = Convert.ToInt32(loc8, 16);" is as that is where I am getting the error said in the title. I have tried researching similar issues others have had with something like this, but it just didn't seem the same and didn't really help me out.</p>
  
  <p>(Btw I am sorry if this doesn't sound so clear, correct me or ask more questions if not understood)</p>
  
  <p><a href="https://i.stack.imgur.com/0L9d5.png" rel="nofollow noreferrer">Screenshot of error when called</a></p>
  
  <p><img src="https://i.stack.imgur.com/0L9d5.png"></p>
  
  <p>My C# Code:</p>
  
  <pre class="lang-cs prettyprint-override"><code>private static string hasher(string input)
          {
              MD5 md5 = System.Security.Cryptography.MD5.Create();
              byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
              byte[] hash = md5.ComputeHash(inputBytes);
  
  
              StringBuilder sb = new StringBuilder();
              for (int i = 0; i &lt; hash.Length; i++)
              {
                  sb.Append(hash[i].ToString("x2"));
              }
  
              return sb.ToString();
  
          }
  
          public static string p(string param1)
          {
              var loc6 = "";
              var loc7 = "";
              var loc8 = "";
              var loc9 = 0;
              var loc2 = hasher(param1);
              var loc4 = 0;
  
              MemoryStream loc0 = new MemoryStream();
              var loc3 = new byte[] { };
              while(loc4 &lt; loc2.Length * 2)
              {
                  loc6 = loc2.Substring(loc4, loc4 + 1);
                  loc7 = loc2.Substring(loc4 + 1, loc4 + 2);
                  loc8 = "0x" + loc6 + loc7;
                  loc9 = Convert.ToInt32(loc8, 16);
                  new BinaryWriter(loc0).Write(loc9);
                  loc4 = loc4 + 2;
              }
              loc0.Position = 0;
              loc3 = loc0.ToArray();
              return Convert.ToBase64String(loc3, 0, 16);
          }
  
          public string calculateFromNewActorCreationData(string username, string password, byte[] small, byte[] full)
          {
              return calculateFromStrings(username, password, small, full);
          }
  
          public string calculateFromStrings(string param1, string param2, object param3, object param4)
          {
              var loc5 = param1 + param2 + fromByteArray(param3 as byte[]) + fromByteArray(param4 as byte[]) + p();
              return p(loc5);
          }
  
          private string fromByteArray(byte[] param1)
          {
              var ms = new MemoryStream(param1);
              List&lt;byte&gt; list2 = new List&lt;byte&gt;();
              if (param1.Length &lt;= 20)
                  return HexStringFromBytes(param1);
              var loc3 = new byte[] { };
              var loc4 = param1.Length / 20;
              var loc5 = 0;
              while (loc5 &lt; 20)
              {
                  ms.Position = loc4 * loc5;
                  list2.Add(new BinaryReader(ms).ReadByte());
                  loc5++;
              }
              loc3 = list2.ToArray();
              return HexStringFromBytes(loc3);
          }
  
          private static string HexStringFromBytes(byte[] bytes)
          {
              var sb = new StringBuilder();
              foreach (byte b in bytes)
              {
                  var hex = b.ToString("x2");
                  sb.Append(hex);
              }
              return sb.ToString();
          }
  
          private string p()
          {
              MemoryStream stream = new MemoryStream();
              new BinaryWriter(stream).Write(120);
              new BinaryWriter(stream).Write(-38);
              new BinaryWriter(stream).Write(99);
              new BinaryWriter(stream).Write(16);
              new BinaryWriter(stream).Write(32);
              new BinaryWriter(stream).Write(51);
              new BinaryWriter(stream).Write(41);
              new BinaryWriter(stream).Write(-110);
              new BinaryWriter(stream).Write(12);
              new BinaryWriter(stream).Write(50);
              new BinaryWriter(stream).Write(81);
              new BinaryWriter(stream).Write(73);
              new BinaryWriter(stream).Write(49);
              new BinaryWriter(stream).Write(-56);
              new BinaryWriter(stream).Write(13);
              new BinaryWriter(stream).Write(48);
              new BinaryWriter(stream).Write(54);
              new BinaryWriter(stream).Write(54);
              new BinaryWriter(stream).Write(14);
              new BinaryWriter(stream).Write(48);
              new BinaryWriter(stream).Write(46);
              new BinaryWriter(stream).Write(2);
              new BinaryWriter(stream).Write(0);
              new BinaryWriter(stream).Write(45);
              new BinaryWriter(stream).Write(-30);
              new BinaryWriter(stream).Write(4);
              new BinaryWriter(stream).Write(-16);
              stream.Position = 0;
              return Encoding.UTF8.GetString(stream.ToArray());
          }
  </code></pre>
  
  <p>ActionScript3 Code:</p>
  
  <pre><code>private static function p(param1:String) : String
        {
           var _loc6_:String = null;
           var _loc7_:String = null;
           var _loc8_:String = null;
           var _loc9_:int = 0;
           var _loc2_:String = MD5.hash(param1);
           var _loc3_:ByteArray = new ByteArray();
           var _loc4_:int = 0;
           while(_loc4_ &lt; _loc2_.length * 2)
           {
              _loc6_ = _loc2_.slice(_loc4_,_loc4_ + 1);
              _loc7_ = _loc2_.slice(_loc4_ + 1,_loc4_ + 2);
              _loc8_ = "0x" + _loc6_ + _loc7_;
              _loc9_ = int(_loc8_);
              _loc3_.writeByte(_loc9_);
              _loc4_ = _loc4_ + 2;
           }
           _loc3_.position = 0;
           var _loc5_:Base64Encoder = new Base64Encoder();
           _loc5_.encodeBytes(_loc3_,0,16);
           return _loc5_.toString();
        }
  
        public function calculateFromNewActorCreationData(param1:NewActorCreationData, param2:ByteArray, param3:ByteArray) : String
        {
           return this.calculateFromStrings(param1.ChosenActorName,param1.ChosenPassword,param2,param3);
        }
  
        public function calculateFromStrings(param1:String, param2:String, param3:Object, param4:Object) : String
        {
           var _loc5_:String = param1 + param2 + this.fromByteArray(param3) + this.fromByteArray(param4) + this.p();
           return p(_loc5_);
        }
  
        private function fromByteArray(param1:Object) : String
        {
           if(param1 == null)
           {
              return "";
           }
           var _loc2_:int = 20;
           if(param1.length &lt;= _loc2_)
           {
              return Hex.fromArray(param1 as ByteArray);
           }
           var _loc3_:ByteArray = new ByteArray();
           var _loc4_:int = param1.length / _loc2_;
           var _loc5_:int = 0;
           while(_loc5_ &lt; _loc2_)
           {
              param1.position = _loc4_ * _loc5_;
              _loc3_.writeByte(param1.readByte());
              _loc5_++;
           }
           return Hex.fromArray(_loc3_);
        }
  
        private function p() : String
        {
           var _loc1_:ByteArray = new ByteArray();
           _loc1_.writeByte(120);
           _loc1_.writeByte(-38);
           _loc1_.writeByte(99);
           _loc1_.writeByte(16);
           _loc1_.writeByte(12);
           _loc1_.writeByte(51);
           _loc1_.writeByte(41);
           _loc1_.writeByte(-118);
           _loc1_.writeByte(12);
           _loc1_.writeByte(50);
           _loc1_.writeByte(81);
           _loc1_.writeByte(73);
           _loc1_.writeByte(49);
           _loc1_.writeByte(-56);
           _loc1_.writeByte(13);
           _loc1_.writeByte(48);
           _loc1_.writeByte(54);
           _loc1_.writeByte(54);
           _loc1_.writeByte(14);
           _loc1_.writeByte(48);
           _loc1_.writeByte(46);
           _loc1_.writeByte(2);
           _loc1_.writeByte(0);
           _loc1_.writeByte(45);
           _loc1_.writeByte(-30);
           _loc1_.writeByte(4);
           _loc1_.writeByte(-16);
           _loc1_.uncompress();
           _loc1_.position = 0;
           return _loc1_.readUTF();
        }
  </code></pre>
  
  <p>What I am expecting in the end is to be able to call the function having the returned Base64 encoded data show in a MessageBox (using messagebox as a test) instead of any errors popping up.</p>
  
  <p>P.S - Besides the main problem I am having with this code, I also feel like the other functions I had converted aren't perfect or just might not be the same. So, if my main problem can be solved, if someone can also double check the other functions of my code make sure they are accurate that would be amazing and thanks in advance.</p>