Posts: 64
Threads: 9
Joined: Dec 2004
Does anybody know of any good routines on how to convert a string to a decimal or hexadecimal number.
For example
If you use A$ = Hex$(509) you get "01FD"
I need to do the reverse.
(related to my previous post on reading the list file)
Perhaps I am having a senior's moment, but ........
t is the End result that matters, not the Tools used to get there.
try this
print val( "&H" + a$ )
Posts: 64
Threads: 9
Joined: Dec 2004
Well done cha0s
At first glance I thought that wouldnt work.
Quote:The VAL function stops reading the string at the first character that
it cannot recognize as part of a number. The VAL function also strips
blanks, tabs, and line feeds from the argument string. For example,
the code below returns the value 1615198
VAL(" 1615 198th Street")
No mention anywhere of &H
Learn something everyday
t is the End result that matters, not the Tools used to get there.
Posts: 6,419
Threads: 74
Joined: Mar 2002
That's implicit. If you check "numeric formats", &H80 is as valid as 134E3 or 56D-2, thus they are considered "valid numbers" by VAL.
Posts: 64
Threads: 9
Joined: Dec 2004
Note the space in front of string
This prints 0 (zero) for second print statement.
Code:
A$ = " 01cf"
print a$
print val("&H"+A$)
Yet
Code:
A$ = "01cf"
print a$
print val("&H"+A$)
prints the decimal value for the second print statement.
BUT
Quote:The VAL function also strips
blanks, tabs, and line feeds from the argument string. For example,
the code below returns the value 1615198
VAL(" 1615 198th Street")
So now Im fused
t is the End result that matters, not the Tools used to get there.
qb bug, apparently. fb messes it up to, actually. (but in a different way) funny, eh
Posts: 544
Threads: 27
Joined: Jan 2005
You had me going there, until I decided to try it for myself.
In my QuickBASIC 4.5, no amount of spaces in front of the hex number interferes with printing the number correctly every time!
Ralph, using QuickBASIC 4.5 and Windows XP Home Edition and Service Pack 2, with HP LaserJet 4L printer.
Posts: 64
Threads: 9
Joined: Dec 2004
Sorry guy's , been bush for a few days.
Well IT IS true.
Code:
A$ = "123"
print val(A$)
A$ = "ABC"
print val("&H" + A$)
A$ = " ABC" 'note space before A
print val("&H" + A$)
This produces
123
2748
0
Code:
BC BasTest1.BAS BasTest1.obj BasTest1.lst /Es /A /E /Fpa /G2 /O /Ot /X /Lr
These switches are what I use on all my programs.
Code:
C:\Program1\Testing>BC BasTest1.BAS BasTest1.obj BasTest1.lst /Es /A /E /Fpa /G2 /O /Ot /X /Lr
Microsoft (R) BASIC Compiler Version 7.10
Copyright (C) Microsoft Corporation 1982-1990. All rights reserved.
45040 Bytes Available
39706 Bytes Free
0 Warning Error(s)
0 Severe Error(s)
Code:
Microsoft (R) Segmented Executable Linker Version 5.60.339 Dec 5 1994
Copyright (C) Microsoft Corp 1984-1993. All rights reserved.
All this on a XP professional platform.
Happy to email the exe to anybody to test on other machines.
t is the End result that matters, not the Tools used to get there.
Posts: 1,845
Threads: 44
Joined: Aug 2002
I dont know if you're looking for this, but this is what I usually use:
[syntax="QBasic"]Print Val("&H" + LtRim$(A$))[/syntax]
This is to also take into account the extra space that might be added by Str$ during the creation of A$.
You probably already knew this though
Val does not strip of spaces, tabs and blank spaces. If you want to strip all spaces, tabs and blank spaces, write a function for it.
- Neo