0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaのコードをSwiftのコードにした時のメモ

Last updated at Posted at 2024-10-04

Javaのbyte[]は、、、

  • byteはSwiftではUInt8
    // ※extension Dataなどに書いてください
    /// バイト配列に変換
    var toByteArray: [UInt8] {
        [UInt8](self)
    }

Javaのshort[]は、、、

  • shortはSwiftではInt16
    // ※extension Dataなどに書いてください
    /// Int16(short)の配列に変換
    var toShortArray: [Int16] {
        var result: [Int16] = []
        let count = self.count
        guard count % MemoryLayout<Int16>.size == 0 else {
            // Int16に変換できないサイズだった
            return result
        }
        for i in stride(from: 0, to: count, by: 2) {
            let shortValue = self.subdata(in: i..<(i+2)).withUnsafeBytes { $0.load(as: Int16.self) }
            result.append(shortValue)
        }
        return result
    }

Javaのintは、、、

  • Javaではintは32bit
  • SwiftではIntは実行環境によって異なるので、Int32をつかう

Javaのlongは、、、

  • SwiftではInt64

  • 参考記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?