There is a more elegant way.
Swift 3:
let str = "Hello"
let buf = UInt8
Swift 4: (thanks to @PJ_Finnegan)
let str = "Hello"
let buf: [UInt8] = Array(str.utf8)
Answer from Albin Stigo on Stack OverflowGitHub
github.com › vapor › core › blob › main › Sources › Bits › ByteBuffer+string.swift
core/Sources/Bits/ByteBuffer+string.swift at main · vapor/core
🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging. - core/Sources/Bits/ByteBuffer+string.swift at main · vapor/core
Author: vapor
`ByteBuffer.set(string:)` returns an `Optional` Int
Int The set(string:at:) (and write(string:)) return an Optional even though they never fail (looking at the implemen... More on github.com
How to convert String to byte in Swift? - Stack Overflow
How to convert String to byte in Swift? Like String .getBytes()in Java. More on stackoverflow.com
[Swift] Return Data from ByteBuffer.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... In pull 7093, ByteBuffer.underlyingBytes was added. More on github.com
Add convenient Type(from buffer: ByteBuffer) initialisers
We can make the process simpler by providing a String(from buffer: ByteBuffer) initialiser. var buffer = ... let string = String(from buffer: buffer)! ... Sign up for free to join this conversation on GitHub. Already have an account? More on github.com
GitHub
github.com › apple › swift-nio › issues › 253
`ByteBuffer.set(string:)` returns an `Optional` Int · Issue #253 · apple/swift-nio
March 28, 2018 - Presumably (and talking to @weissi ) this is a left-over from an old variant of the func which supported different encodings. Also: the comment doesn't say anything about the optionality, making it even more confusing. Does it mean no buffer space is left? Does it mean the encoding failed? Does it mean Mercury passed Venus? Since the buffer is auto-expanding, I think this should just not fail and return a plain Int. ByteBuffer.set(string: String, at index: Int) -> Int?
Author: apple
Forked by 2 users
Languages: Swift 100.0% | Swift 100.0%
Top answer 1 of 7
132
There is a more elegant way.
Swift 3:
let str = "Hello"
let buf = UInt8
Swift 4: (thanks to @PJ_Finnegan)
let str = "Hello"
let buf: [UInt8] = Array(str.utf8)
2 of 7
18
You can iterate through the UTF8 code points and create an array:
var str = "hello, world"
var byteArray = Byte
for char in str.utf8{
byteArray += [char]
}
println(byteArray)
Dot Net Perls
dotnetperls.com › convert-string-byte-array-swift
Swift - Convert String to Byte Array - Dot Net Perls
September 17, 2024 - In Swift 5.9 a byte is called a UInt8—an unsigned 8 bit integer. A byte array is a UInt8 array. In ASCII we can treat chars as UInt8 values. With the utf8 String property, we get a UTF8View collection. We can convert this to a byte array. Then we can manipulate bytes.
Starred by 40 users
Forked by 3 users
Languages: Swift 100.0% | Swift 100.0%
Swift Forums
forums.swift.org › related projects › swiftnio
Reading invalid strings from ByteBuffer - SwiftNIO - Swift Forums
May 6, 2021 - I noticed that the ByteBuffer for reading strings are not fallible. This means that I can create an invalid string like so: let invalidUTF8 = context.channel.allocator.buffer(bytes: [0xc3, 0x28]) let invalidString = Str…
Swift Forums
forums.swift.org › using swift
Java String.getBytes() equivalent in swift - Using Swift - Swift Forums
June 13, 2018 - Is there any way to convert string into bytes in swift. currently i try to convert String into Data type and then try to get bytes from it .but the result is not the same with java String.getBytes() does. Any Help . Th…
Top answer 1 of 15
115
Update for Swift 3/Xcode 8:
String from bytes: [UInt8]:
if let string = String(bytes: bytes, encoding: .utf8) {
print(string)
} else {
print("not a valid UTF-8 sequence")
}
String from data: Data:
let data: Data = ...
if let string = String(data: data, encoding: .utf8) {
print(string)
} else {
print("not a valid UTF-8 sequence")
}
Update for Swift 2/Xcode 7:
String from bytes: [UInt8]:
if let string = String(bytes: bytes, encoding: NSUTF8StringEncoding) {
print(string)
} else {
print("not a valid UTF-8 sequence")
}
String from data: NSData:
let data: NSData = ...
if let str = String(data: data, encoding: NSUTF8StringEncoding) {
print(str)
} else {
print("not a valid UTF-8 sequence")
}
Previous answer:
String does not have a stringWithBytes() method.
NSString has a
NSString(bytes: , length: , encoding: )
method which you could use, but you can create the string directly from NSData, without the need for an UInt8 array:
if let str = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
println(str)
} else {
println("not a valid UTF-8 sequence")
}
2 of 15
24
Update for Swift 5.2.2:
String(decoding: yourByteArray, as: UTF8.self)
Apple Developer
developer.apple.com › forums › thread › 660595
Converting Binary String back to original string using Swift
I have one module that is supposed to encode a string to binary and another module that, when passed the encoded binary string, is supposed to to decode that string and print the original string. In my first module file, I have the encoding process that works.