Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hex string to Decimal
#1
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.
Reply
#2
try this


print val( "&H" + a$ )
Reply
#3
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.
Reply
#4
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.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
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.
Reply
#6
qb bug, apparently. fb messes it up to, actually. (but in a different way) funny, eh
Reply
#7
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.
Reply
#8
Quote: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!

you're right. i didn't even test it because i figured why would someone post if it wasn't true?


ah well, the fb version bugs on that case anyways. it spits out a negative number.
Reply
#9
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.
Reply
#10
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 Wink 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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)