From 9ac5ad90a562edff229eea9c842f23afcf4685e8 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 17 Aug 2011 02:57:19 -0400 Subject: [PATCH] fixing issue #176, need to use integer math to count digits --- j/string.j | 15 ++++++++++++++- test/tests.j | 4 ++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/j/string.j b/j/string.j index f875740d70f36..0e3f6fb227544 100644 --- a/j/string.j +++ b/j/string.j @@ -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}, diff --git a/test/tests.j b/test/tests.j index 5c988bea52198..57f8f09406a0b 100644 --- a/test/tests.j +++ b/test/tests.j @@ -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