Swift translate string to special Data buffer

Multi tool use
up vote
-2
down vote
favorite
I am having trouble figuring out how to translate a string (named str
) into a Data buffer (named strData
) that can be decoded using this particular process: strData.reduce("", {$0 + String(format: "%02X", $1)})
import Foundation
let str = "Hello world"
let strData: Data = str.data(using: .utf8)! // Wrong. What here?
let decoded = strData.reduce("", {$0 + String(format: "%02X", $1)})
print(decoded == str)
The print statement should print true
.
This is Testable here.
Thank you.
swift
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
-2
down vote
favorite
I am having trouble figuring out how to translate a string (named str
) into a Data buffer (named strData
) that can be decoded using this particular process: strData.reduce("", {$0 + String(format: "%02X", $1)})
import Foundation
let str = "Hello world"
let strData: Data = str.data(using: .utf8)! // Wrong. What here?
let decoded = strData.reduce("", {$0 + String(format: "%02X", $1)})
print(decoded == str)
The print statement should print true
.
This is Testable here.
Thank you.
swift
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?
– Ricky Mo
Nov 5 at 3:40
I need to feed a known string value to a function that uses this particular.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.
– ajf-
Nov 5 at 3:44
That.reduce
method always generate a hex string representation of the data, which will never equal to the original string.
– Ricky Mo
Nov 5 at 3:48
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
If the original string is a hex representation, you can easily find some hex to data method to turn it intoData
, like this https://stackoverflow.com/a/26503955/10317684
– Ricky Mo
Nov 5 at 3:55
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I am having trouble figuring out how to translate a string (named str
) into a Data buffer (named strData
) that can be decoded using this particular process: strData.reduce("", {$0 + String(format: "%02X", $1)})
import Foundation
let str = "Hello world"
let strData: Data = str.data(using: .utf8)! // Wrong. What here?
let decoded = strData.reduce("", {$0 + String(format: "%02X", $1)})
print(decoded == str)
The print statement should print true
.
This is Testable here.
Thank you.
swift
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am having trouble figuring out how to translate a string (named str
) into a Data buffer (named strData
) that can be decoded using this particular process: strData.reduce("", {$0 + String(format: "%02X", $1)})
import Foundation
let str = "Hello world"
let strData: Data = str.data(using: .utf8)! // Wrong. What here?
let decoded = strData.reduce("", {$0 + String(format: "%02X", $1)})
print(decoded == str)
The print statement should print true
.
This is Testable here.
Thank you.
swift
swift
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 5 at 3:48
Joel
1,4425719
1,4425719
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 5 at 3:35


ajf-
1001
1001
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
ajf- is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?
– Ricky Mo
Nov 5 at 3:40
I need to feed a known string value to a function that uses this particular.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.
– ajf-
Nov 5 at 3:44
That.reduce
method always generate a hex string representation of the data, which will never equal to the original string.
– Ricky Mo
Nov 5 at 3:48
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
If the original string is a hex representation, you can easily find some hex to data method to turn it intoData
, like this https://stackoverflow.com/a/26503955/10317684
– Ricky Mo
Nov 5 at 3:55
add a comment |
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?
– Ricky Mo
Nov 5 at 3:40
I need to feed a known string value to a function that uses this particular.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.
– ajf-
Nov 5 at 3:44
That.reduce
method always generate a hex string representation of the data, which will never equal to the original string.
– Ricky Mo
Nov 5 at 3:48
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
If the original string is a hex representation, you can easily find some hex to data method to turn it intoData
, like this https://stackoverflow.com/a/26503955/10317684
– Ricky Mo
Nov 5 at 3:55
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?– Ricky Mo
Nov 5 at 3:40
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?– Ricky Mo
Nov 5 at 3:40
I need to feed a known string value to a function that uses this particular
.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.– ajf-
Nov 5 at 3:44
I need to feed a known string value to a function that uses this particular
.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.– ajf-
Nov 5 at 3:44
That
.reduce
method always generate a hex string representation of the data, which will never equal to the original string.– Ricky Mo
Nov 5 at 3:48
That
.reduce
method always generate a hex string representation of the data, which will never equal to the original string.– Ricky Mo
Nov 5 at 3:48
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
If the original string is a hex representation, you can easily find some hex to data method to turn it into
Data
, like this https://stackoverflow.com/a/26503955/10317684– Ricky Mo
Nov 5 at 3:55
If the original string is a hex representation, you can easily find some hex to data method to turn it into
Data
, like this https://stackoverflow.com/a/26503955/10317684– Ricky Mo
Nov 5 at 3:55
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
ajf- is a new contributor. Be nice, and check out our Code of Conduct.
ajf- is a new contributor. Be nice, and check out our Code of Conduct.
ajf- is a new contributor. Be nice, and check out our Code of Conduct.
ajf- is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147978%2fswift-translate-string-to-special-data-buffer%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
POYJm,ym5It5yjQvJ saZQkbi608jmnSSk
decoded
is obviously a bunch of hex characters, which never equals "Hello world". What do you actually want to compare?– Ricky Mo
Nov 5 at 3:40
I need to feed a known string value to a function that uses this particular
.reduce
in order to retrieve it. The library assumes the data will be fed using a Data buffer, so I need to convert my string into it.– ajf-
Nov 5 at 3:44
That
.reduce
method always generate a hex string representation of the data, which will never equal to the original string.– Ricky Mo
Nov 5 at 3:48
Assuming the original string is a hex representation, then, would this be possible? or would the decoded value not equal the original hex value?
– ajf-
Nov 5 at 3:50
If the original string is a hex representation, you can easily find some hex to data method to turn it into
Data
, like this https://stackoverflow.com/a/26503955/10317684– Ricky Mo
Nov 5 at 3:55