strtoul() Function- Swift

I'm trying to create a swift iOS program that converts a number into dec, bin, and hex numbers. I've come across the strtoul function, but don't quite understand how to use it, would someone be able to explain it? Thanks!


The method strtoul is pretty simple to use. You will need also to use String(radix:()) to convert it to the other direction. You can create an extension to convert from hexaToDecimal or from binaryToDecimal as follow:

Usage String(radix:())

extension Int { var toBinary: String { return String(self, radix: 2) } var toHexa: String { return String(self, radix: 16) } }

Usage strtoul()

extension String { var hexaToDecimal: Int { return Int(strtoul(self, nil, 16)) } var hexaToBinary: String { return hexaToDecimal.toBinary } var binaryToDecimal: Int { return Int(strtoul(self, nil, 2)) } var binaryToHexa: String { return binaryToDecimal.toHexa } }


let myBinFromInt = 255.toBinary // "11111111" let myhexaFromInt = 255.toHexa // "ff" let myIntFromHexa = "ff".hexaToDecimal // 255 let myBinFromHexa = "ff".hexaToBinary // "11111111" let myIntFromBin = "11111111".binaryToDecimal // 255 let myHexaFromBin = "11111111".binaryToHexa // "ff"


The strtoul() function converts the string in str to an unsigned long value. The conversion is done according to the given base, which must be between 2 and 36 inclusive, or be the special value 0.

Really it sounds like you want to use NSString

From what it sounds like, you want to convert an unsigned integer to decimal, hex and binary.

For example, if you had an integer n:

var st = NSString(format:"%2X", n)

would convert the integer to hexadecimal and store it in the variable st.

//NSString(format:"%2X", 10) would give you 'A' as 10 is A in hex //NSString(format:"%2X", 17) would give you 11 as 17 is 11 in hex


var st = NSString(format:"%u", n)

Decimal (2 decimal places)

var st = NSString(format:"%.02f", n)


  • How do you create a loop that reads ints by their individual character
  • Three-way xor-like function
  • Dijkstra adjacency list
  • java convert to int
  • Printing binary values in Java
  • how to calculate distance between two points in google maps?
  • Put percent labels next to legend instead of in the slice
  • HABTM associations in Rails : collecting and counting the categories of a model's children
  • button in popup.html not working
  • Timing loops with asynchronous functions
  • VB.NET 2012 Property Set on Property Get
  • Visualizing a large matrix in matlab
  • how do you obtain the address of an instance after overriding the __str__ method in python
  • Convert two columns Pandas data frame to dictionary of list with first column as keys
  • How to extract text from a PDF and decode characters?
  • Cannot page through all results using nextPageToken on YouTube search API v3
  • Guava how to copy all files from one directory to another
  • C++ cout and enum representations
  • How to know which Linq statement produced the SQL on hand during runtime?
  • EntLib Way to Bind “Null” Value to Parameter
  • is there a way to update filter with async data
  • DateTime.Today for a specific time zone
  • C++ Single function pointer for all template instances
  • Can I have a variable number of URI parameters or key-value pairs in Laravel 4?
  • Easiest way to get current unix timestamp via XSL
  • Doctrine/Symfony entity generator and generating entity from one table
  • Casting between Interfaces and Classes
  • jwtBearer bearer token with rc-1 update to ASP.Net 5
  • Enabling DTD support in Sql Server
  • Convert SQLite database to XML
  • How to get current document uri in XSLT?
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • Conversion from string “a” to type 'Boolean' is not valid
  • Test if a set exists before trying to drop it
  • Django: Count of Group Elements
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++