Results 1 to 3 of 3

Thread: Curious side effect of data type casting in VAL() function ?

  1. #1
    Member
    Join Date
    Jan 2018
    Location
    France
    Age
    65
    Posts
    40
    Rep Power
    5

    Curious side effect of data type casting in VAL() function ?

    Hi,

    The following thin basic example is from a program wich extracts data from a microprocessor development board.
    The uploaded file contains formatted lines. The first field, on 4 chars is the hexadecimal address of data in the following fields.
    ex: 02AF 01A23F65E47CD089

    The data is entered in a string array , at the address extracted from first field. Extracted as a 4 char string. Dim hex_addr As String * 4
    The addresses are gathered without any order.

    the string array is dimensioned as 2^16 pairs of hex chars, so the position of a byte in the array is
    (addresse*2)+1
    The position in the array from 1 to 131072 has to be coded in a dword integer.

    To convert the 4 hex chars address I use pos_in_array = Val("&h" & hex_addr )

    But when the address is = 0x7fff the returned value is 32767
    = 0x8000 the returned value is 4294934528 , Val("&h8000) is considered as negative !

    The workaround I found is to add a zero to the string before conversion.

    See examples
     Uses "Console"
    
    Dim hex_addr   As String * 4
    Dim  data_array as string * 131072
    Dim pos_in_array_w   As  Word
    Dim pos_in_array_dw   As DWord
    
    hex_addr   = "4000"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w , pos_in_array_dw
    
    hex_addr   = "7FFf"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w  , pos_in_array_dw
    
    hex_addr   = "8000"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w  , pos_in_array_dw
    
    hex_addr   = "fff0"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w  , pos_in_array_dw
    
    hex_addr   = "8000"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h0" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w  , pos_in_array_dw
    
    hex_addr   = "fff0"
    pos_in_array_w = Val( "&h" & hex_addr   )
    pos_in_array_dw = Val( "&h0" & hex_addr   )
    PrintL hex_addr   ,, pos_in_array_w  , pos_in_array_dw
     
     PrintL "---------------"
    pos_in_array_w = Val( "&h8000" )
    pos_in_array_dw = Val( "&h8000" )
    PrintL "&h8000" ,, pos_in_array_w  , pos_in_array_dw
    
    pos_in_array_w = Val( "&h08000" )
    pos_in_array_dw = Val( "&h08000" )
    PrintL "&h08000" ,, pos_in_array_w  , pos_in_array_dw
    
    PrintL "direct"
    PrintL "&h08000" ,, Val( "&h08000" ) 
    PrintL "&h8000" ,, Val( "&h8000" ) 
    
    
    PrintL "Press a key to end program"
    WaitKey
    
    Best regards.

    Dany
    Last edited by dco045; 06-04-2018 at 18:23.

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    6,839
    Blog Entries
    3
    Rep Power
    697
    Hi Dany,

    this is by design, inherited from convention in PowerBASIC. But I agree it should be mentioned in the documentation.


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 7 64bit - Intel Core 2 Duo T6600 @ 2.2GHz - 4 GB RAM - NVIDIA GeForce G210M 512MB
    Windows 8 64bit - Intel Core i5-3350P @ 3.1GHz - 8 GB RAM - NVIDIA GeForce GT640 3GB

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    51
    Posts
    8,343
    Blog Entries
    2
    Rep Power
    10
    Yes, sorry: I wanted to reply but forgot it.
    I will change help and possibly add some specific HEX functions.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

Similar Threads

  1. Strings and data type
    By Henry in forum File
    Replies: 2
    Last Post: 28-06-2011, 12:56
  2. FreeBasic SDK: extended data type
    By ErosOlmi in forum Module SDK (Freebasic version)
    Replies: 29
    Last Post: 15-09-2008, 20:47
  3. New in next thinBasic: numeric type casting
    By ErosOlmi in forum thinBasic vaporware
    Replies: 9
    Last Post: 18-07-2008, 21:11
  4. Most efficient integer data type for small numbers?
    By Randall in forum thinBasic General
    Replies: 4
    Last Post: 12-09-2007, 03:57
  5. How to create a new "fake" data type?
    By tbNewbie in forum Module SDK (GCC C /C++)
    Replies: 3
    Last Post: 13-02-2006, 18:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •