iosswiftwindows-1251

how can I convert String to Data


I have next:

deviceName = String.init(bytes: temp.prefix(upTo: index), encoding: .windowsCP1251)   

where temp - [UInt8]. My question is: how can I convert this string back to Data?
I'm trying to convert like this:

newDataName = Data(newName.windowsCP1251)   

But result is:

Value of type String has no member windowsCP1251

It is works with converting using utf8, but it shows russian characters incorrect. I need to use windowsCP1251 only:

newDataName = Data(newName.utf8)   

Solution

  • Maybe this is what you looking for:

    newDataName = newName.data(using: .windowsCP1251)