# Printing large hexadecimal values

**URL:** <https://forums.swift.org/t/printing-large-hexadecimal-values/2761>\
**Category:** Using Swift\
**Created:** [May 25, 2016, 5:16pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761 "2016-05-25T17:16:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Burgett](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@Ken\_Burgett](https://forums.swift.org/u/Ken_Burgett)\
**Post date:** [May 25, 2016, 5:16pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/1 "2016-05-25T17:16:18Z")

</div>

I really shouldn't have to ask this question for a variety of other languages, but print format conversion in Swift a black box to me.

I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I can't find any documentation on this. Please advise.

> **···**
>
> --  
> Ken Burgett  
> Principal Software Engineer  
> Email: kenb@iotone.io  
> Office: 530.693.4449  
> Mobile: 831.332.6846  
> URL: [www.iotone.co](http://www.iotone.co)

---

<div class="post-metadata">

**Author:** ![snej](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/snej/32/365_2.png) [@snej](https://forums.swift.org/u/snej)\
**Post date:** [May 25, 2016, 5:54pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/2 "2016-05-25T17:54:13Z")

</div>

print(String(format: “%llx”, n)) // typed from memory

—Jens

> **···**
>
> > On May 25, 2016, at 10:16 AM, Ken Burgett via swift-users \<swift-users@swift.org\> wrote:
> > 
> > I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I can't find any documentation on this. Please advise.

---

<div class="post-metadata">

**Author:** ![Barry\_Gilhuly](https://avatars.discourse-cdn.com/v4/letter/b/82dd89/32.png) [@Barry\_Gilhuly](https://forums.swift.org/u/Barry_Gilhuly)\
**Post date:** [May 25, 2016, 5:46pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/3 "2016-05-25T17:46:43Z")

</div>

String() has a format: init variant:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let myNum = 0x85f3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let numString = String(format:"%08x", myNum)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print( "The number is: \(numString)")

// prints: The number is: 000085f3

Although, in playground (7.3.1, swift 2.2) this doesn't work quite right:  
// prints: The number is: ("%08x", 34291)\n"

> **···**
>
> On 5/25/16 1:16 PM, Ken Burgett via swift-users wrote:
> 
> > I really shouldn't have to ask this question for a variety of other languages, but print format conversion in Swift a black box to me.
> > 
> > I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I can't find any documentation on this. Please advise.

---

<div class="post-metadata">

**Author:** ![knatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/knatt/32/22335_2.png) [@knatt](https://forums.swift.org/u/knatt)\
**Post date:** [May 25, 2016, 6:26pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/4 "2016-05-25T18:26:35Z")

</div>

“%08x” to zero-pad to 8 characters, “%08X" for uppercase A–F

% cat main.swift  
import Foundation  
let n = 10597059  
print(String(format: "%x", n))  
print(String(format: "%08x", n))  
print(String(format: "%08X", n))  
% swift main.swift  
a1b2c3  
00a1b2c3  
00A1B2C3  
%

> **···**
>
> > On May 25, 2016, at 10:54 AM, Jens Alfke via swift-users \<swift-users@swift.org\> wrote:
> > 
> > > On May 25, 2016, at 10:16 AM, Ken Burgett via swift-users \<swift-users@swift.org\> wrote:
> > > 
> > > I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I can't find any documentation on this. Please advise.
> > 
> > print(String(format: “%llx”, n)) // typed from memory
> > 
> > —Jens  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Ken\_Burgett](https://avatars.discourse-cdn.com/v4/letter/k/49beb7/32.png) [@Ken\_Burgett](https://forums.swift.org/u/Ken_Burgett)\
**Post date:** [May 25, 2016, 7:10pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/5 "2016-05-25T19:10:19Z")

</div>

Hi Jens,

You are correct, the "%llx" works for UInt64, while "%16x" does not. "%llX" also works, producing an uppercase string.

Should this be reported as a bug?

> **···**
>
> On 2016-05-25 12:00, Jens Alfke wrote:
> 
> > > On May 25, 2016, at 11:11 AM, Ken Burgett \<kenb@iotone.io\> wrote:
> > > 
> > > the "%llx" field is not getting interpreted...
> > 
> > You have to import Foundation to bring in the String.init(format:…)  
> > method, which is bridged from Foundation's NSString class.  
> > (This is a temporary inconvenience until the Swift standard library is  
> > complete.)
> > 
> > —Jens
> 
> --  
> Ken Burgett  
> Principal Software Engineer  
> Email: kenb@iotone.io  
> Office: 530.693.4449  
> Mobile: 831.332.6846  
> URL: [www.iotone.co](http://www.iotone.co)

---

<div class="post-metadata">

**Author:** ![knatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/knatt/32/22335_2.png) [@knatt](https://forums.swift.org/u/knatt)\
**Post date:** [May 25, 2016, 8:16pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/6 "2016-05-25T20:16:10Z")

</div>

%16x pads with spaces instead of zeros, use %016X for uppercase zero-padded output. `man 3 printf` will show the full spec (minus the objc-specific %@), or Apple probably documents it somewhere. It breaks down like this:

- x/X is a format saying it’s an int type and should be printed in hex with lowercase/uppercase a-f.  
- ll is a flag saying the int is a long long.  
- 16 says the output from that format spec should be 16 characters wide (right-aligned by default)  
- 0 says left-pad with zeros instead of the default spaces.

> **···**
>
> > On May 25, 2016, at 12:10 PM, Ken Burgett \<kenb@iotone.io\> wrote:
> > 
> > On 2016-05-25 12:00, Jens Alfke wrote:
> > 
> > > > On May 25, 2016, at 11:11 AM, Ken Burgett \<kenb@iotone.io\> wrote:  
> > > > the "%llx" field is not getting interpreted...
> > > 
> > > You have to import Foundation to bring in the String.init(format:…)  
> > > method, which is bridged from Foundation's NSString class.  
> > > (This is a temporary inconvenience until the Swift standard library is  
> > > complete.)  
> > > —Jens
> > 
> > Hi Jens,
> > 
> > You are correct, the "%llx" works for UInt64, while "%16x" does not. "%llX" also works, producing an uppercase string.
> > 
> > Should this be reported as a bug?  
> > --  
> > Ken Burgett  
> > Principal Software Engineer  
> > Email: kenb@iotone.io  
> > Office: 530.693.4449  
> > Mobile: 831.332.6846  
> > URL: [www.iotone.co](http://www.iotone.co)

---

<div class="post-metadata">

**Author:** ![snej](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/snej/32/365_2.png) [@snej](https://forums.swift.org/u/snej)\
**Post date:** [May 25, 2016, 8:19pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/7 "2016-05-25T20:19:31Z")

</div>

No, it’s correct behavior. You need to use the “ll” ("long long") modifier to specify that the parameter is a 64-bit value.

—Jens

> **···**
>
> > On May 25, 2016, at 12:10 PM, Ken Burgett \<kenb@iotone.io\> wrote:
> > 
> > You are correct, the "%llx" works for UInt64, while "%16x" does not. "%llX" also works, producing an uppercase string.  
> > Should this be reported as a bug?

---

<div class="post-metadata">

**Author:** ![jtbandes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jtbandes/32/463_2.png) [@jtbandes](https://forums.swift.org/u/jtbandes)\
**Post date:** [May 25, 2016, 8:49pm UTC](https://forums.swift.org/t/printing-large-hexadecimal-values/2761/8 "2016-05-25T20:49:34Z")

</div>

I'm not sure this is strictly necessary, but I think it's a good idea to  
explicitly use the "C\*" types when doing formatting with varargs, to make  
sure the calling convention matches what the callee expects:

&nbsp;&nbsp;&nbsp;&nbsp;String(format: "%llx", CLongLong(myNumber))

> **···**
>
> On Wed, May 25, 2016 at 1:19 PM, Jens Alfke via swift-users \< swift-users@swift.org\> wrote:
> 
> > On May 25, 2016, at 12:10 PM, Ken Burgett \<kenb@iotone.io\> wrote:
> > 
> > You are correct, the "%llx" works for UInt64, while "%16x" does not.  
> > "%llX" also works, producing an uppercase string.  
> > Should this be reported as a bug?
> > 
> > No, it’s correct behavior. You need to use the “ll” ("long long") modifier  
> > to specify that the parameter is a 64-bit value.
> > 
> > —Jens
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
