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 - 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
🌐
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
Find elsewhere
🌐
GitHub
github.com › google › flatbuffers › issues › 7169
[Swift] Return Data from ByteBuffer. · Issue #7169 · google/flatbuffers
March 12, 2022 - In pull 7093, ByteBuffer.underlyingBytes was added. Is there a reason this API returns an Array[UInt8] rather than the more common Data? The JSONEncoder returns a Data and in almost every case where I'd like to use serialized data, it wo...
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.
🌐
GitHub
github.com › ajotaos › swift-bytes
GitHub - ajotaos/swift-bytes
.package(url: "https://github.... 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) ...
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?
🌐
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 - Working with bytes in Swift 4 Sometimes there is a need to implement an algorithm for encryption/decryption on strings, files or other data in your Swift project. Conversion for this type of …
🌐
GitHub
github.com › mochidev › Bytes
GitHub - mochidev/Bytes: Swift Library for working with sequences of Bytes (aka [UInt8])
Bytes aims to be your go-to Swift library for transforming basic types to and from buffers of bytes that can be serialized and written to the network, a file, or a database.
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…
🌐
GitHub
github.com › apple › swift-nio › issues › 1870
Improve ByteBuffer/ByteBufferView equability · Issue #1870 · apple/swift-nio
June 3, 2021 - apple / swift-nio Public · Notifications · You must be signed in to change notification settings · Fork 740 · Star 8.4k · New issueCopy link · New issueCopy link · Closed · Closed · Improve ByteBuffer/ByteBufferView equability#1870 · Copy link · Assignees ·
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
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.