How to convert array of bytes [UInt8] into hexa string in Swift












5















I found how to convert hexa string into bytes [UInt8] but I have not found how to convert bytes [UInt8] into an hexa string in Swift



this hexstring convert to string code:



static func bytesConvertToHexstring(byte : [UInt8]) -> String {
var string = ""

for val in byte {
//getBytes(&byte, range: NSMakeRange(i, 1))
string = string + String(format: "%02X", val)
}

return string
}


samething like this result:



"F063C52A6FF7C8904D3F6E379EB85714ECA9C1CB1E8DFD6CA5D3B4A991269D60F607C565C327BD0ECC0985F74E5007E0D276499E1ADB4E0C92D8BDBB46E57705B2D5390FF5CBD4ED1B850C537301CA7E"


UInt8 array: [0, 11, 8, 15, 6, 6, 5, 8, 8, 4, 14, 14, 0, 0, 9, 12, 6, 4, 10, 6, 4, 8, 6, 2, 14, 2, 6, 13, 3, 3, 12, 4, 3, 12, 8, 13, 14, 4, 10, 1, 12, 15, 4, 0, 14, 14, 0, 8, 8, 14, 6, 15, 2, 2, 9, 15, 13, 6, 2, 6, 8, 15, 4, 2, 12, 1, 0, 13, 13, 4, 6, 0, 9, 6, 8, 2, 7, 0, 6, 1, 3, 3, 9, 15, 5, 7, 12, 8, 7, 5, 13, 14, 15, 6, 7, 6, 12, 6, 7, 7, 11, 9, 6, 0, 14, 5, 6, 14, 1, 5, 13, 10, 12, 13, 14, 2, 13, 14, 4, 7, 13, 0, 3, 10, 6, 11, 9, 12, 7, 11, 5, 3, 5, 11, 4, 9, 6, 10, 14, 0, 11, 7, 15, 9, 3, 14, 5, 1, 10, 14, 5, 6, 12, 4, 12, 14, 4, 3, 9, 8, 0]










share|improve this question




















  • 1





    Your bytes array and result don't match. How is 0 converted to an F in hex?

    – Code Different
    Nov 5 '15 at 14:40











  • Something like this stackoverflow.com/a/30198375/1187415 ?

    – Martin R
    Nov 5 '15 at 15:08











  • @Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

    – Clever
    Nov 6 '15 at 5:17
















5















I found how to convert hexa string into bytes [UInt8] but I have not found how to convert bytes [UInt8] into an hexa string in Swift



this hexstring convert to string code:



static func bytesConvertToHexstring(byte : [UInt8]) -> String {
var string = ""

for val in byte {
//getBytes(&byte, range: NSMakeRange(i, 1))
string = string + String(format: "%02X", val)
}

return string
}


samething like this result:



"F063C52A6FF7C8904D3F6E379EB85714ECA9C1CB1E8DFD6CA5D3B4A991269D60F607C565C327BD0ECC0985F74E5007E0D276499E1ADB4E0C92D8BDBB46E57705B2D5390FF5CBD4ED1B850C537301CA7E"


UInt8 array: [0, 11, 8, 15, 6, 6, 5, 8, 8, 4, 14, 14, 0, 0, 9, 12, 6, 4, 10, 6, 4, 8, 6, 2, 14, 2, 6, 13, 3, 3, 12, 4, 3, 12, 8, 13, 14, 4, 10, 1, 12, 15, 4, 0, 14, 14, 0, 8, 8, 14, 6, 15, 2, 2, 9, 15, 13, 6, 2, 6, 8, 15, 4, 2, 12, 1, 0, 13, 13, 4, 6, 0, 9, 6, 8, 2, 7, 0, 6, 1, 3, 3, 9, 15, 5, 7, 12, 8, 7, 5, 13, 14, 15, 6, 7, 6, 12, 6, 7, 7, 11, 9, 6, 0, 14, 5, 6, 14, 1, 5, 13, 10, 12, 13, 14, 2, 13, 14, 4, 7, 13, 0, 3, 10, 6, 11, 9, 12, 7, 11, 5, 3, 5, 11, 4, 9, 6, 10, 14, 0, 11, 7, 15, 9, 3, 14, 5, 1, 10, 14, 5, 6, 12, 4, 12, 14, 4, 3, 9, 8, 0]










share|improve this question




















  • 1





    Your bytes array and result don't match. How is 0 converted to an F in hex?

    – Code Different
    Nov 5 '15 at 14:40











  • Something like this stackoverflow.com/a/30198375/1187415 ?

    – Martin R
    Nov 5 '15 at 15:08











  • @Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

    – Clever
    Nov 6 '15 at 5:17














5












5








5








I found how to convert hexa string into bytes [UInt8] but I have not found how to convert bytes [UInt8] into an hexa string in Swift



this hexstring convert to string code:



static func bytesConvertToHexstring(byte : [UInt8]) -> String {
var string = ""

for val in byte {
//getBytes(&byte, range: NSMakeRange(i, 1))
string = string + String(format: "%02X", val)
}

return string
}


samething like this result:



"F063C52A6FF7C8904D3F6E379EB85714ECA9C1CB1E8DFD6CA5D3B4A991269D60F607C565C327BD0ECC0985F74E5007E0D276499E1ADB4E0C92D8BDBB46E57705B2D5390FF5CBD4ED1B850C537301CA7E"


UInt8 array: [0, 11, 8, 15, 6, 6, 5, 8, 8, 4, 14, 14, 0, 0, 9, 12, 6, 4, 10, 6, 4, 8, 6, 2, 14, 2, 6, 13, 3, 3, 12, 4, 3, 12, 8, 13, 14, 4, 10, 1, 12, 15, 4, 0, 14, 14, 0, 8, 8, 14, 6, 15, 2, 2, 9, 15, 13, 6, 2, 6, 8, 15, 4, 2, 12, 1, 0, 13, 13, 4, 6, 0, 9, 6, 8, 2, 7, 0, 6, 1, 3, 3, 9, 15, 5, 7, 12, 8, 7, 5, 13, 14, 15, 6, 7, 6, 12, 6, 7, 7, 11, 9, 6, 0, 14, 5, 6, 14, 1, 5, 13, 10, 12, 13, 14, 2, 13, 14, 4, 7, 13, 0, 3, 10, 6, 11, 9, 12, 7, 11, 5, 3, 5, 11, 4, 9, 6, 10, 14, 0, 11, 7, 15, 9, 3, 14, 5, 1, 10, 14, 5, 6, 12, 4, 12, 14, 4, 3, 9, 8, 0]










share|improve this question
















I found how to convert hexa string into bytes [UInt8] but I have not found how to convert bytes [UInt8] into an hexa string in Swift



this hexstring convert to string code:



static func bytesConvertToHexstring(byte : [UInt8]) -> String {
var string = ""

for val in byte {
//getBytes(&byte, range: NSMakeRange(i, 1))
string = string + String(format: "%02X", val)
}

return string
}


samething like this result:



"F063C52A6FF7C8904D3F6E379EB85714ECA9C1CB1E8DFD6CA5D3B4A991269D60F607C565C327BD0ECC0985F74E5007E0D276499E1ADB4E0C92D8BDBB46E57705B2D5390FF5CBD4ED1B850C537301CA7E"


UInt8 array: [0, 11, 8, 15, 6, 6, 5, 8, 8, 4, 14, 14, 0, 0, 9, 12, 6, 4, 10, 6, 4, 8, 6, 2, 14, 2, 6, 13, 3, 3, 12, 4, 3, 12, 8, 13, 14, 4, 10, 1, 12, 15, 4, 0, 14, 14, 0, 8, 8, 14, 6, 15, 2, 2, 9, 15, 13, 6, 2, 6, 8, 15, 4, 2, 12, 1, 0, 13, 13, 4, 6, 0, 9, 6, 8, 2, 7, 0, 6, 1, 3, 3, 9, 15, 5, 7, 12, 8, 7, 5, 13, 14, 15, 6, 7, 6, 12, 6, 7, 7, 11, 9, 6, 0, 14, 5, 6, 14, 1, 5, 13, 10, 12, 13, 14, 2, 13, 14, 4, 7, 13, 0, 3, 10, 6, 11, 9, 12, 7, 11, 5, 3, 5, 11, 4, 9, 6, 10, 14, 0, 11, 7, 15, 9, 3, 14, 5, 1, 10, 14, 5, 6, 12, 4, 12, 14, 4, 3, 9, 8, 0]







swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 16 '17 at 5:28









Leo Dabus

132k30269345




132k30269345










asked Nov 5 '15 at 14:21









CleverClever

77312




77312








  • 1





    Your bytes array and result don't match. How is 0 converted to an F in hex?

    – Code Different
    Nov 5 '15 at 14:40











  • Something like this stackoverflow.com/a/30198375/1187415 ?

    – Martin R
    Nov 5 '15 at 15:08











  • @Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

    – Clever
    Nov 6 '15 at 5:17














  • 1





    Your bytes array and result don't match. How is 0 converted to an F in hex?

    – Code Different
    Nov 5 '15 at 14:40











  • Something like this stackoverflow.com/a/30198375/1187415 ?

    – Martin R
    Nov 5 '15 at 15:08











  • @Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

    – Clever
    Nov 6 '15 at 5:17








1




1





Your bytes array and result don't match. How is 0 converted to an F in hex?

– Code Different
Nov 5 '15 at 14:40





Your bytes array and result don't match. How is 0 converted to an F in hex?

– Code Different
Nov 5 '15 at 14:40













Something like this stackoverflow.com/a/30198375/1187415 ?

– Martin R
Nov 5 '15 at 15:08





Something like this stackoverflow.com/a/30198375/1187415 ?

– Martin R
Nov 5 '15 at 15:08













@Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

– Clever
Nov 6 '15 at 5:17





@Zoff Dino, Yes you are right. I give some example code. 'string convert hex string' method work correct

– Clever
Nov 6 '15 at 5:17












1 Answer
1






active

oldest

votes


















15














Xcode 9 or later • Swift 4 or later



extension String {
var hexaBytes: [UInt8] {
var position = startIndex
return (0..<count/2).flatMap { _ in // for Swift 4.1 or later use compactMap instead of flatMap
defer { position = index(position, offsetBy: 2) }
return UInt8(self[position...index(after: position)], radix: 16)
}
}
var hexaData: Data { return hexaBytes.data }
}

extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var hexa: String {
return map{ String(format: "%02X", $0) }.joined()
}
}




"0f00ff".hexaBytes           // [15, 0, 255]
"0f00ff".hexaData // 3 bytes
"0f00ff".hexaData.hexa // "0F00FF"
"0f00ff".hexaData as NSData // <0f00ff>





share|improve this answer


























  • thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

    – Clever
    Nov 6 '15 at 5:21






  • 1





    Very elegant. Only thing better would be to add the reverse as well.

    – Roy Falk
    Mar 21 '16 at 11:52











  • Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

    – zaph
    Jun 8 '16 at 11:56











  • I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

    – zaph
    Jun 9 '16 at 12:41











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',
autoActivateHeartbeat: false,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33546967%2fhow-to-convert-array-of-bytes-uint8-into-hexa-string-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









15














Xcode 9 or later • Swift 4 or later



extension String {
var hexaBytes: [UInt8] {
var position = startIndex
return (0..<count/2).flatMap { _ in // for Swift 4.1 or later use compactMap instead of flatMap
defer { position = index(position, offsetBy: 2) }
return UInt8(self[position...index(after: position)], radix: 16)
}
}
var hexaData: Data { return hexaBytes.data }
}

extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var hexa: String {
return map{ String(format: "%02X", $0) }.joined()
}
}




"0f00ff".hexaBytes           // [15, 0, 255]
"0f00ff".hexaData // 3 bytes
"0f00ff".hexaData.hexa // "0F00FF"
"0f00ff".hexaData as NSData // <0f00ff>





share|improve this answer


























  • thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

    – Clever
    Nov 6 '15 at 5:21






  • 1





    Very elegant. Only thing better would be to add the reverse as well.

    – Roy Falk
    Mar 21 '16 at 11:52











  • Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

    – zaph
    Jun 8 '16 at 11:56











  • I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

    – zaph
    Jun 9 '16 at 12:41
















15














Xcode 9 or later • Swift 4 or later



extension String {
var hexaBytes: [UInt8] {
var position = startIndex
return (0..<count/2).flatMap { _ in // for Swift 4.1 or later use compactMap instead of flatMap
defer { position = index(position, offsetBy: 2) }
return UInt8(self[position...index(after: position)], radix: 16)
}
}
var hexaData: Data { return hexaBytes.data }
}

extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var hexa: String {
return map{ String(format: "%02X", $0) }.joined()
}
}




"0f00ff".hexaBytes           // [15, 0, 255]
"0f00ff".hexaData // 3 bytes
"0f00ff".hexaData.hexa // "0F00FF"
"0f00ff".hexaData as NSData // <0f00ff>





share|improve this answer


























  • thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

    – Clever
    Nov 6 '15 at 5:21






  • 1





    Very elegant. Only thing better would be to add the reverse as well.

    – Roy Falk
    Mar 21 '16 at 11:52











  • Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

    – zaph
    Jun 8 '16 at 11:56











  • I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

    – zaph
    Jun 9 '16 at 12:41














15












15








15







Xcode 9 or later • Swift 4 or later



extension String {
var hexaBytes: [UInt8] {
var position = startIndex
return (0..<count/2).flatMap { _ in // for Swift 4.1 or later use compactMap instead of flatMap
defer { position = index(position, offsetBy: 2) }
return UInt8(self[position...index(after: position)], radix: 16)
}
}
var hexaData: Data { return hexaBytes.data }
}

extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var hexa: String {
return map{ String(format: "%02X", $0) }.joined()
}
}




"0f00ff".hexaBytes           // [15, 0, 255]
"0f00ff".hexaData // 3 bytes
"0f00ff".hexaData.hexa // "0F00FF"
"0f00ff".hexaData as NSData // <0f00ff>





share|improve this answer















Xcode 9 or later • Swift 4 or later



extension String {
var hexaBytes: [UInt8] {
var position = startIndex
return (0..<count/2).flatMap { _ in // for Swift 4.1 or later use compactMap instead of flatMap
defer { position = index(position, offsetBy: 2) }
return UInt8(self[position...index(after: position)], radix: 16)
}
}
var hexaData: Data { return hexaBytes.data }
}

extension Collection where Element == UInt8 {
var data: Data {
return Data(self)
}
var hexa: String {
return map{ String(format: "%02X", $0) }.joined()
}
}




"0f00ff".hexaBytes           // [15, 0, 255]
"0f00ff".hexaData // 3 bytes
"0f00ff".hexaData.hexa // "0F00FF"
"0f00ff".hexaData as NSData // <0f00ff>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 11:06

























answered Nov 5 '15 at 15:14









Leo DabusLeo Dabus

132k30269345




132k30269345













  • thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

    – Clever
    Nov 6 '15 at 5:21






  • 1





    Very elegant. Only thing better would be to add the reverse as well.

    – Roy Falk
    Mar 21 '16 at 11:52











  • Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

    – zaph
    Jun 8 '16 at 11:56











  • I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

    – zaph
    Jun 9 '16 at 12:41



















  • thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

    – Clever
    Nov 6 '15 at 5:21






  • 1





    Very elegant. Only thing better would be to add the reverse as well.

    – Roy Falk
    Mar 21 '16 at 11:52











  • Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

    – zaph
    Jun 8 '16 at 11:56











  • I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

    – zaph
    Jun 9 '16 at 12:41

















thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

– Clever
Nov 6 '15 at 5:21





thanks answer but i need bytes array convert to string or hexstring convert to string. give me same example plz

– Clever
Nov 6 '15 at 5:21




1




1





Very elegant. Only thing better would be to add the reverse as well.

– Roy Falk
Mar 21 '16 at 11:52





Very elegant. Only thing better would be to add the reverse as well.

– Roy Falk
Mar 21 '16 at 11:52













Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

– zaph
Jun 8 '16 at 11:56





Great but readability of the hex string would be better with spaces every 8 characters: 6162636465666768696a6b6c6d6e6f70 to 61626364 65666768 696a6b6c 6d6e6f70. But I guess Swift hates hex displays.

– zaph
Jun 8 '16 at 11:56













I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

– zaph
Jun 9 '16 at 12:41





I am aware of that and do use it. What I find interesting is that Apple has switched from hex to decimal in displaying data with Swift and for many cases that just does not work as well.

– zaph
Jun 9 '16 at 12:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33546967%2fhow-to-convert-array-of-bytes-uint8-into-hexa-string-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation