Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-2514] Add a "hex dump" method to Data #4115

Open
martinr448 opened this issue Aug 29, 2016 · 5 comments
Open

[SR-2514] Add a "hex dump" method to Data #4115

martinr448 opened this issue Aug 29, 2016 · 5 comments

Comments

@martinr448
Copy link

martinr448 commented Aug 29, 2016

Previous ID SR-2514
Radar None
Original Reporter @martinr448
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Foundation
Labels Improvement
Assignee None
Priority Medium

md5: 641850a22679507d5d233e1d7021576d

Issue Description:

There should be a way to get the contents of a Data object in hexadecimal form.
The description methods do not provide that, as it was the case with NSData:

let data = Data(bytes: [1, 2, 3])
print(data) // 3 bytes
print(data.debugDescription) // 3 bytes

Of course I can cast to NSData

print(data as NSData) // <010203>

but it would be nice to have a "native" method. Suggestions:

  • description returns a hex dump, or

  • debugDescription returns a hex dump, or

  • a new hexEncodedString method, similar to the existing Base64 methods.

@beccadax
Copy link
Contributor

beccadax commented Sep 23, 2016

It's worth noting that, if Data.description returned a hex dump (or some other complete representation of the instance's contents), then we could provide a compatible Data.init(_: String) and then conform Data to LosslessStringConvertible.

@parkera
Copy link
Member

parkera commented Sep 30, 2016

We do have a bug in radar for this (15719610).

@martinr448
Copy link
Author

martinr448 commented Jun 13, 2018

Casting to NSData does not help any more in Xcode 10/Swift 4.2:

    let data1 = Data(bytes: [1, 2, 3])
    print(data1 as NSData) // Foundation._NSSwiftData
    
    let data2 = "Hello world".data(using: .utf8)!
    print(data2 as NSData) // _NSInlineData

@parkera
Copy link
Member

parkera commented Jun 14, 2018

I'd be open to accepting a PR that improves struct Data's debugDescription to print out some actual bytes.

@martinr448
Copy link
Author

martinr448 commented Jun 26, 2019

On macOS 10.15 (Catalina) beta, the NSData output is now truncated:

let data = Data(1...255)
// 255 bytes
print(data as NSData)
// {length = 255, bytes = 0x01020304 05060708 090a0b0c 0d0e0f10 ... f8f9fafb fcfdfeff }

so that this workaround does not help anymore to get the (full) content of a Data value (for debugging purposes).

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants