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
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.
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 › 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…
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
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