title: Elegant method to convert UInt8 array of variable length to Integers
body: <p>Suppose I have one <code>UInt8</code> array of variable length and the lengths can be of 1, 2, 4 and 8 and it can be either signed or unsigned. Target is to convert this array to corresponding Integer.</p>
  <p>Following is my best try but it looks ugly as there is apparent code duplication. Is there any elegant method to solve this problem? Any suggestions are appreciated.</p>
  <pre class="lang-swift prettyprint-override"><code>func fromByteArray&lt;T&gt;(_ value: [UInt8], _: T.Type) -&gt; T {
      return value.withUnsafeBytes { $0.baseAddress!.load(as: T.self) }
  }
  
  func convert(_ hexList[UInt8], _ isSigned: Bool) -&gt; Any {
      if is_signed {
              switch hexList.count {
              case 1: return Int(fromByteArray(hexList) as Int8)
              case 2: return Int(fromByteArray(hexList) as Int16)
              case 4: return Int(fromByteArray(hexList) as Int32)
              case 8: return Int(fromByteArray(hexList) as Int64)
              default: return 0
              }
      } else {
              switch hexList.count {
              case 1: return UInt(fromByteArray(hexList) as UInt8)
              case 2: return UInt(fromByteArray(hexList) as UInt16)
              case 4: return UInt(fromByteArray(hexList) as UInt32)
              case 8: return UInt(fromByteArray(hexList) as UInt64)
              default: return 0
              }
      }
  }
  </code></pre>