Printing large hexadecimal values

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

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.

String() has a format: init variant:

                 let myNum = 0x85f3
                 let numString = String(format:"%08x", myNum)
                 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.

“%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

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

%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

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?

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:

    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