Android Question ParseInt not returning proper value

Robert Valentino

Well-Known Member
Licensed User
Longtime User
When I run the following hex value 0x01000000 into Bit.ParseInt I do not get what I thought I would

Hex Value:0x01000000 Comes back as :1.6777216E7

Bit.ParseInt("01000000") should return return 16777216

What is even stranger to me is I am assigning this valid to an Int and it is giving me a floating point number.

If I assign the Bit.ParseInt to a Long it returns the right value
But If I then try and put that long into a Int it again becomes a floating point number.

16777216 is well withing the range of and Int (Int : 4bytes signed: -2147483648 to 2147483647)

So I am confused. What am I doing wrong??

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Just realized that this is NOT a ParseInt problem but a Debugger Log statement

When I do the following Log statement

B4X:
#if Debug
                                           Log("Hex ID:" &FileLineParms(j) &"  ValidID:" &ValidDateID.ValidID)                                          
#End If

Hex ID:0x00114000 ValidID:1130496
Hex ID:0x00000102 ValidID:258
Hex ID:0x00000104 ValidID:260
Hex ID:0x00000108 ValidID:264
Hex ID:0x01000000 ValidID:1.6777216E7

ValidDateID.ValidID is a INT and should be printing a whole number but for some reason is printing a decimal number.

I guess I will need to do a NumberFormat to print these out. Strange I never saw this happening before.

Sorry for these posts.
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
but for some reason is printing a decimal number.
1.6777216E7
That is an INTEGER !
1.6777216 Exponentiated to7!
How ever, reading this will help you understand why its being represented that way

Anyway, to solve your log issue, convert the int to string before you log it, like

dim stringvalue as string = myintvalue
log(stringvalue)
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Yes it is an Integer but it is printing like a float or double. Pretty much what your article said about the Math.Pow function returning a double

I do not think assigning it to a string would help as I said (probably at the same time you were posting) that I would need to use NumberFormat.
Haven't tried it but I think assigning it to a string put in a string "1.6777216E7"

Just surprised that our debugger doesn't recognize that I am printing an INTEGER and format it for me (In Log statements that is)
Probably something I should add to the WISH list.
This misrepresentation of the data on the screen (while shooting a different bug) just caused me to leap and look around at things that weren't a problem because all the other values the debugger was printing were smaller so they printed as straight decimal numbers instead of a floating point number. Just got confused

Should of guessed I was having a Senior moment with me turning 67 yesterday.

All's good now - thanks for your feedback
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
B4X:
    Type  TValidDateID(ValidDate As String, ValidID As Int)           '  Is in Class_Globals


    '  FileLineParms is a List containing the following values   <some text string>,   0x00114000, 0x00000102, 0x00000104, 0x00000108, 0x01000000

     For  j = 1 To FileLineParms.Length-1                           
           If  FileLineParms(j).StartsWith("0x") Then
               Dim ValidDateID As TValidDateID                                       
                                       
               ValidDateID.Initialize
               ValidDateID.ValidDate = UseDate
                                       
               ValidDateID.ValidID   = Bit.ParseInt(FileLineParms(j).Trim.SubString(2), 16)

#if Debug
                Log("Hex ID:" &FileLineParms(j) &"  ValidID:" &ValidDateID.ValidID)                                       
#End If

                ....              ' More code no need to show for example

            end if
    next

for the values shown the Log statement produces the following output:

Hex ID:0x00114000 ValidID:1130496
Hex ID:0x00000102 ValidID:258
Hex ID:0x00000104 ValidID:260
Hex ID:0x00000108 ValidID:264
Hex ID:0x01000000 ValidID:1.6777216E7
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Here is a small example.

This is what I found in testing (see logs below)

That when I just compile and run the program it seems to produce the right results but if I change the log statement (add some extra formatting spaces) the results are wrong

B4X:
Logger connected to:  samsung SM-T210R
--------- beginning of /dev/log/main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Hex ID:0x00114000    ValidID:1130496
Hex ID:0x00000102    ValidID:258
Hex ID:0x00000104    ValidID:260
Hex ID:0x00000108    ValidID:264
Hex ID:0x01000000    ValidID:16777216


Now I change the log statement (add or remove some spaces before ParseIn[)
B4X:
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
** Service (starter) Destroy **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Hex ID[0x00114000]    ParseIn[00114000]  ValidID:1130496
Hex ID[0x00000102]    ParseIn[00000102]  ValidID:258
Hex ID[0x00000104]    ParseIn[00000104]  ValidID:260
Hex ID[0x00000108]    ParseIn[00000108]  ValidID:264
Hex ID[0x01000000]    ParseIn[01000000]  ValidID:1.6777216E7

Changing the code anywhere else seems to be ok, but changing the log statement seems to cause this problem (NOTE: Of course we are running in Debug Mode)

This should be real fun to debug - love to hear the problem

BobVal
 

Attachments

  • TestParseInt.zip
    6.9 KB · Views: 177
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I understand and realize that.

Like I said, it caught my attention while shooting another bug and just lead me astray for a while (just another hourly senior moment)

Thanks
 
Upvote 0
Top