Valulng, Vallng, Valuint, Valint hex notation

New to FreeBASIC? Post your questions here.
Post Reply
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Valulng, Vallng, Valuint, Valint hex notation

Post by fzabkar »

I'm having difficulty understanding the purpose of the Valulng, Vallng, Valuint, Valint keywords. I understand what they do, but I don't understand why they go about in the way that they do. Specifically, I don't understand why a hexadecimal string is expected to have a prefix of "&H". Why not allow for the accepted standard notation, namely a prefix of "0x" or a suffix of "h"?

I have an application which accepts hexadecimal strings in the form 0x1234 and then converts them to ulongints. In order for this to work, I need to use Mid to change the first two characters of the string. This begs the question, are there any circumstances where external data are formatted with FB's hex notation? As for an FB hex string generated by the program, I can't see why one would need to subsequently convert it to its original numeric value.
robert
Posts: 169
Joined: Aug 06, 2019 18:45

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by robert »

fzabkar wrote:I'm having difficulty understanding the purpose of the Valulng, Vallng, Valuint, Valint keywords. I understand what they do, but I don't understand why they go about in the way that they do. Specifically, I don't understand why a hexadecimal string is expected to have a prefix of "&H". Why not allow for the accepted standard notation, namely a prefix of "0x" or a suffix of "h"?

I have an application which accepts hexadecimal strings in the form 0x1234 and then converts them to ulongints. In order for this to work, I need to use Mid to change the first two characters of the string. This begs the question, are there any circumstances where external data are formatted with FB's hex notation? As for an FB hex string generated by the program, I can't see why one would need to subsequently convert it to its original numeric value.
The "standard notation" for BASIC is the &H prefix. MSX BASIC, QuickBASIC, Visual Basic, PowerBASIC and FreeBASIC all prefix hexadecimal numbers with &H.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by fzabkar »

Perhaps I didn't explain myself very well.

I understand that "&H" is standard BASIC notation. However, I ask myself, under what circumstances would my program execute the following code?

Code: Select all

ulivar = Valulng( "&H1234")
Does it make sense for a string such as "&H1234" to be input from a file? What kind of real file would store hex data in Basic notation? I can't think of any, so there is unlikely to be any use for the following code when dealing with hex/binary/octal data:

Code: Select all

Line Input #inf, strgvar
uliVar = Valulng( strgvar )
If the string doesn't come from an external source, then it must be generated internally by the program. This then begs the question, does it make sense for the string to be generated within the Basic program and then converted back again within the same program? If the program can generate the string, then it can also directly generate the ulongint ...

Code: Select all

ulivar1 = &H1234
strgvar = Hex( ulivar1 )
...
ulivar2 = Valulng( strgvar )
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by MrSwiss »

fzabkar wrote:I understand that "&H" is standard BASIC notation. However, I ask myself, under what circumstances would my program execute the following code?
fzabkar wrote:Does it make sense for a string such as "&H1234" to be input from a file?
Why is it that some people make "simple things" unnecessarily complex?

Let's face it all those data-type conversions (especially to/from file) are not really needed.
Store decimal values to file and thereafter, retrieve them as such again.
All that's required is a (specified) delimiter between two values, in order to separate them (CSV style file).
(typically uses either comma ',' or semi-colon ';' as delimiter-character)

Btw. "0x" notation is C/C++ not BASIC.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by MrSwiss »

There is another thing that I'd like to bring to attention:

the 'more recent' conversion functions of FB are (not as misleading as the val... stuff):
unsigned:
CUByte()
CUShort()
CULng()
CULngInt("1234567890") and all the others Cxxxx types (have a look in the doc's).
They make use of VAL... conversions but, behind the scenes.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by fzabkar »

I'm converting a ddrescue map file into another format.

Ddrescue produces a human readable log (map), eg ...

Code: Select all

# Mapfile. Created by GNU ddrescue version 1.25
# Command line: ddrescue -d -c18 /dev/fd0 fdimage mapfile
# Start time:   2015-07-21 09:37:44
# Current time: 2015-07-21 09:38:19
# Copying non-tried blocks... Pass 1 (forwards)
# current_pos  current_status  current_pass
0x00120000     ?               1
# pos   size  status
0x00000000  0x00117000  +
0x00117000  0x00000200  -
0x00117200  0x00001000  /
0x00118200  0x00007E00  *
0x00120000  0x00048000  ?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Valulng, Vallng, Valuint, Valint hex notation

Post by jj2007 »

In case you can find an external source that uses &H1234 notation, let me know, maybe I can add it to the formats that Val() recognises.

P.S. That was easy, just 20 minutes of work:

Code: Select all

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Print Str$("&H075BCD15\t%i\n", Val("&H075BCD15"))	; BASIC: &H...
  Print Str$("&h075BCD15\t%i\n", Val("&h075BCD15"))	; BASIC: &h...
  Print Str$("0x075BCD15\t%i\n", Val("0x075BCD15"))	; C/C++: 0x...
  Print Str$("$075BCD15\t%i\n", Val("$075BCD15"))	; C/C++: $...
  Print Str$("075BCD15h\t%i\n", Val("075BCD15h"))	; Masm: ...h
EndOfCode
Output:

Code: Select all

&H075BCD15      123456789
&h075BCD15      123456789
0x075BCD15      123456789
$075BCD15       123456789
075BCD15h       123456789
Post Reply