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 Overflow
🌐
GitHub
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
Discussions

`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
🌐 github.com
15
March 28, 2018
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
🌐 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
🌐 github.com
4
March 12, 2022
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.com
1
March 20, 2020
🌐
GitHub
gist.github.com › lzell › c0747a18742ca2dd59bfb969ce35e768
Swift string to data (bytes) and back · GitHub
Swift string to data (bytes) and back · Raw · string_to_data_back.swift · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
github.com › apple › swift-nio › issues › 253
`ByteBuffer.set(string:)` returns an `Optional` Int · Issue #253 · apple/swift-nio
March 28, 2018 - Int The set(string:at:) (and write(string:)) return an Optional even though they never fail (looking at the implemen...
Author: apple
🌐
GitHub
github.com › jaz303 › ByteBuffer.swift
GitHub - jaz303/ByteBuffer.swift
February 27, 2016 - Create a ByteBuffer with given length. Return the length of the buffer, in bytes. String representation of byte buffer's contents, for diagnostic purposes. Read element ix from the buffer. Write element ix to the buffer.
Forked by 2 users
Languages: Swift 100.0% | Swift 100.0%
🌐
GitHub
github.com › apple › swift-nio › blob › main › Sources › NIOCore › ByteBuffer-aux.swift
swift-nio/Sources/NIOCore/ByteBuffer-aux.swift at main · apple/swift-nio
/// This will allocate a new `ByteBuffer` with enough space to fit `string` and potentially some extra space using ... /// - info: If you have access to a `Channel`, `ChannelHandlerContext`, or `ByteBufferAllocator` we
Author: apple
Author: LIFX
Find elsewhere
🌐
GitHub
github.com › google › flatbuffers › issues › 7169
[Swift] Return Data from ByteBuffer. · Issue #7169 · google/flatbuffers
March 12, 2022 - 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.
Author: google
🌐
GitHub
github.com › apple › swift-nio › issues › 1456
Add convenient Type(from buffer: ByteBuffer) initialisers · Issue #1456 · apple/swift-nio
March 20, 2020 - 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?
Author: apple
🌐
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.
🌐
Medium
gorjanshukov.medium.com › working-with-bytes-in-ios-swift-4-de316a389a0c
Working with bytes in Swift 4. Sometimes there is a need to implement… | by Gorjan Shukov | Medium
October 30, 2020 - In Swift a Character can be constructed from a UnicodeScalar value. And we can build a UnicodeScalar from an Int. This allows many conversions. With utf8 and utf16, both properties on a String, we can access all Int values of the characters of a String.
🌐
GitHub
github.com › ajotaos › swift-bytes
GitHub - ajotaos/swift-bytes
.package(url: "https://github.com/ajotaos/swift-bytes.git", from: "1.0.0") ... Here's a quick example showing how to create a ByteBuffer, write some bytes to it, and then read those bytes back: var buffer = ByteBuffer() buffer.write(bytes: [0x41, 0x42, 0x43]) let bytes = buffer.bytes(count: 3) print(bytes) // Output: [65, 66, 67] You can also write integers, floating-point numbers, and strings to a ByteBuffer:
Author: ajotaos
🌐
Swift Forums
forums.swift.org › using swift
How to convert String to byte array? - Using Swift - Swift Forums
March 8, 2022 - I would like to take a String: let myStr = "This is a string" And take the first n bytes from it: let firstNBytes = ... I'm not even sure what data type holds bytes in Swift, Data? UInt8?
🌐
Swift Forums
forums.swift.org › related projects › swiftnio
Reading invalid strings from ByteBuffer - SwiftNIO - Swift Forums
May 6, 2021 - This means that I can create an invalid string like so: let invalidUTF8 = context.channel.allocator.buffer(bytes: [0xc3, 0x28]) let invalidString = String(buffer: invalidUTF8) Creating a String directly from the array of bytes ...
🌐
GitHub
github.com › apple › swift-nio › issues › 1870
Improve ByteBuffer/ByteBufferView equability · Issue #1870 · apple/swift-nio
June 3, 2021 - XCTAssert(buffer.readableBytesView == ByteBufferView([0x05, 0x00])) XCTAssert(buffer == ByteBuffer(bytes: [0x05, 0x00])) I think we should provide a shorthand convenience, e.g. XCTAssert(buffer == [0x05, 0x00]) XCTAssert(buffer.readableBytesView == [0x05, 0x00]) ... good first issueGood for newcomersGood for newcomerskind/enhancementImprovements to existing feature.Improvements to existing feature.
Author: apple
🌐
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…
🌐
Apple Developer
developer.apple.com › forums › thread › 660595
Converting Binary String back to original string using Swift
With your code, the Result output contains line break characters at each end of the binary string, so I used this String as Result outpuut. Using resultOutput, you can write something like this: By the way, your code can be shortened utilizing the functionality of Swift and Swift Standard Library.
🌐
Swift Package Index
swiftpackageindex.com › apple › swift-nio › 2.68.0 › documentation › NIOCore › ByteBuffer
swift-nio Documentation – Swift Package Index
June 27, 2024 - This documentation is from a previous release and may not reflect the latest released version. View latest release documentation · [object Module] · Last updated on 27 Jun 2024 · The Swift Package Index is entirely funded by sponsorship. Thank you to all our sponsors for their generosity