Ideas for how to do this cleanly? Current code below... Thanks!
# Convert a non-negative integer i into an octet string.
function i2osp(x::Integer, len = nothing)
if typeof(len) <: Integer && (x >= 256^len)
throw("integer is too large")
end
if x < 0
throw("integer is negative")
end
bytes = Uint8[]
while x > 0
b = uint8(x & 0xff)
push!(bytes, b)
x = x >>> 8
end
str = convert(ASCIIString, reverse(bytes)) # Fails if any byte value > 127
if typeof(len) <: Integer && (length(str) < len)
str = repeat("\0", len - str) * str
end
return str
end