Swift文字列を[UInt8]バイト配列として取得し、Cコードから同じバイト配列を返して、オリジナルスウィフトストリング。私はユニコードを維持しようとしています(変換/操作に損失がないので)。私は、エラーが「復号化機能で変換ラインに期待される引数の型 『UINT』にタイプ 『の値String.Encoding』を変換できません取得しています。任意の助けを事前に感謝を!Swift文字列をCバイト配列に変換してSwift文字列に戻す
// function to encrypt a string with custom "C" code and encode the result into a hex string
func EncryptString(password: String, stringToEncrypt: String) ->String {
var hexStr = ""
// convert the String password into utf8 bytes
let pw = password.data(using: String.Encoding.utf8, allowLossyConversion:false)
var passwordBytes : [UInt8] = Array(pw!)
// convert the string to encrypt into utf8 bytes
let bytes = stringToEncrypt.data(using: String.Encoding.utf8, allowLossyConversion:false)
var buf : [UInt8] = Array(bytes!)
// encrypt the string to encrypt with the Unicode password (both are Unicode in Swift)
encryptData(&passwordBytes, Int32(password.count), &buf, Int32(stringToEncrypt.count))
// turn the now encrypted "stringToEncrypt" into two character hex values in a string
for byte in buf {
hexStr.append(String(format:"%2X", byte))
}
// return the encrypted hex encoded string to the caller...
return hexStr
}
func DecryptString(password: String, hexStringToDecrypt: String) ->String {
var decryptedStr = ""
// convert the String password into utf8 bytes
let pw = password.data(using: String.Encoding.utf8, allowLossyConversion:false)
var passwordBytes : [UInt8] = Array(pw!)
// convert the string to encrypt into utf8 bytes
let bytes = hexStringToDecrypt.data(using: String.Encoding.utf8, allowLossyConversion:false)
var buf : [UInt8] = Array(bytes!)
// encrypt the string to encrypt with the Unicode password (both are Unicode in Swift)
let bytecount = password.count
decryptData(&passwordBytes, Int32(password.count), &buf, Int32(hexStringToDecrypt.count))
// turn the now encrypted "hexStringToDecrypt" into int values here is where I get error: Cannot convert value of type 'String.Encoding' to expected argument type 'UInt'
var unicode_str = NSString(bytes: buf, length: bytecount, encoding: NSUTF32LittleEndianStringEncoding)
// return the encrypted hex encoded string to the caller...
return unicode_str
}
}
をスウィフト3/4で
エラーを引き起こす正確な行を指摘してください。 'utf8'を使用しようとする行がいくつかあります。 Swift 3以降を使用していることを確認します。 – rmaddy
ありがとうございます!私はSwift 4を使用していますが、現在のコード行は次のようになります:var unicode_str = NSString(buf、length:bytecount、encoding:NSUTF32LittleEndianStringEncoding) – user6096790
「Cバイト配列」は正確には何ですか?バイトが言語関連を持たないと言うことができるので、単純に8ビット(最近)です。スウィフト "C"もありません。 – zaph