BinaryInteger

extension BinaryInteger
  • Creates a new Integer value from the given Boolean.

    Declaration

    Swift

    public init(_ value: Bool)

    Parameters

    value

    A Bool used to initialise the value.

  • Returns a String of the binary representation zero padded to the width of the value.

    The value passed is converted to its String representation using base-16 and then zero padded upto the width of the parameter if necessary. A UInt8 will be padded to 8 characters, a UInt64 will be padded upto 64 characters.

      UInt8(1).binary()         // '00000001'
      UInt8(0x12).binary()      // '00010011'
      UInt16(0x1234).binary()   // '0001001000110100'
    

    The separator flag determines if a _ should be added between every 4 digits.

      UInt8(1).binary(separator: true)          // '0000_0001'
      UInt16(0x1234).binary(separator: true)    // '0001_0010_0011_0100'
    

    Declaration

    Swift

    public func binary(separators: Bool = false) -> String

    Parameters

    value

    The numeric value to convert.

    separators

    Boolean flag to control _ added between every 4 digits. The default is false.

    Return Value

    The zero padded value as a String.

  • Returns a String of the octal representation zero padded to the width of the value.

    The value passed is converted to its String representation using base-8 and then zero padded upto the width of the parameter if necessary. A UInt8 will be padded to 3 characters, a UInt64 will be padded upto 22 characters.

      UInt8(1).octal()          // '00000001'
      UInt8(0x12).octal()       // '00010011'
      UInt16(0x1234).octal()    // '0001001000110100'
    

    The separator flag determines if a _ should be added between every 4 digits.

      UInt8(1).octal(separator: true)          // '0000_0001'
      UInt16(0x1234).octal(separator: true)    // '0001_0010_0011_0100'
    

    Declaration

    Swift

    public func octal(separators: Bool = false) -> String

    Parameters

    value

    The numeric value to convert.

    separators

    Boolean flag to control _ added between every 4 digits. The default is false.

    Return Value

    The zero padded value as a String.

  • Returns a String of the hexadecimal representation zero padded to the width of the value.

    The value passed is converted to its String representation using base-16 and then zero padded upto the width of the parameter if necessary. A UInt8 will be padded to 2 characters, a UInt64 will be padded upto 16 characters.

      UInt8(1).hex()          // '01'
      UInt8(0x12).hex()       // '12'
      UInt32(0x1234).hex()    // '00001234'
    

    The separator flag determines if a _ should be added between every 4 digits.

      UInt8(1).hex(separator: true)         // '01'
      UInt32(0x1234).hex(separator: true)   // '0000_1234'
    

    Declaration

    Swift

    public func hex(separators: Bool = false) -> String

    Parameters

    value

    The numeric value to convert.

    separators

    Boolean flag to control _ added between every 4 digits. The default is false.

    Return Value

    The zero padded value as a String.