Swift translate string to special Data buffer











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.










share|improve this question









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 into Data, like this https://stackoverflow.com/a/26503955/10317684
    – Ricky Mo
    Nov 5 at 3:55

















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.










share|improve this question









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 into Data, like this https://stackoverflow.com/a/26503955/10317684
    – Ricky Mo
    Nov 5 at 3:55















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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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 into Data, 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












  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






ajf- is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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





































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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()