Skip to content

Commit

Permalink
fixing issue #176, need to use integer math to count digits
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Aug 17, 2011
1 parent f8b5f95 commit 9ac5ad9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 14 additions & 1 deletion j/string.j
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,23 @@ uint64(s::String) = parse_int(Uint64, s, 10)

## integer to string functions ##

function ndigits(n::Int, b::Int)
nd = 1
ba = convert(typeof(n), b)
while true
n = div(n, ba)
if n == 0
break
end
nd += 1
end
return nd
end

function uint2str(n::Int, b::Int)
if n < zero(n); error("uint2str: negative argument ", n); end
if b < 2; error("uint2str: invalid base ", b); end
ndig = n==convert(typeof(n),0) ? 1 : int32(floor(log(n)/log(b)+1))
ndig = ndigits(n, b)
sz = convert(Size, ndig+1)
data = Array(Uint8, sz)
ccall(:uint2str, Ptr{Uint8},
Expand Down
4 changes: 4 additions & 0 deletions test/tests.j
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ else
end
@assert isa((()->box(Char,unbox32(int32(65))))(), Char)
@assert hex(0xffffffffffff)=="ffffffffffff"
@assert hex(0xffffffffffff+1)=="1000000000000"
@assert hex(typemax(Uint64))=="ffffffffffffffff"
# conversions
function fooo()
local x::Int8
Expand Down

0 comments on commit 9ac5ad9

Please sign in to comment.