### Author Topic: Use VAL function with binary &B prefixed number strings  (Read 186 times)

#### RhoSigma

• Jr. Member
• Posts: 75
• 4 * 1.0079 = 4.0026
##### Use VAL function with binary &B prefixed number strings
« on: February 28, 2013, 03:52:44 PM »
Hi all,

you certainly all know about the VAL function and its usage, maybe some fewer people know also that it can convert octal &O and hexadecimal &H prefixed number strings. However it lacks the ability to use it for binary &B prefixed numbers.

I've made a small modification to the "libqbx.cpp" file located in the "internal\c" subdirectory of the QB64 v0.954 (SDL branch) install directory to add the missing &B functionality. It's mainly a copy&paste of the &O and &H stuff modified for the &B usage.

Here's how to do it:

1. backup your original "libqbx.cpp" file
2. open "libqbx.cpp" in any editor
3. search for "hex:" (line 13107 in unmodified v0.954 libqbx.cpp), it should be the only occurence of this label
4. locate the following line (3 lines down)
Code: [Select]
`if ((c==79)||(c==111)){//"O"or"o"`5. insert the following code snippet right above that line
Code: [Select]
`if ((c==66)||(c==98)){//"B"or"b" hex_digits=0; hex_value=0; for (i=i+2;i<s->len;i++){ c=s->chr[i]; if ((c>=48)&&(c<=49)){//0-1 c-=48; hex_value<<=1; hex_value|=c; if (hex_digits||c) hex_digits++;  if (hex_digits>64) {error(6); return 0;} }else break; }//i return hex_value;}`6. save the file
7. recompile the file by executing the batch file "makelib.bat" located in the same directory

Now you can use VAL with &B or &b number strings:
Code: [Select]
`PRINT VAL("&B1101") 'should print "13"`
Regards, RhoSigma
Polygon functions: http://rhosigma-cw.net/down/GfxDemos.zip
Screen savers: http://rhosigma-cw.net/down/ScrSavers.zip
Libraries collection: http://rhosigma-cw.net/down/QB64Library.zip (extract into the QB64 folder, a new sub-folder "QB64Library" is created)

#### OlDosLover

• Hero Member
• Posts: 3860
• OlDosLover
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #1 on: March 01, 2013, 12:02:04 AM »
Hi all,
Thanks RhoSigma. I've never used it that way.
OlDosLover.

#### codeguy

• Hero Member
• Posts: 3552
• what the h3ll did i name that code?
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #2 on: March 01, 2013, 09:33:49 AM »
or you can modify one of my base conversion subs to "roll your own" using qb64.
this is the code, qb64-style: http://www.qb64.net/forum/index.php?topic=1260.msg9076#msg9076
« Last Edit: March 01, 2013, 09:41:00 AM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

#### Clippy

• Hero Member
• Posts: 16440
• I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #3 on: March 01, 2013, 01:06:29 PM »
This is another functionality that was never finished in QB64. I've seldom seen anybody but Fatman use &B as we are so used to working around Qbasic's _BIT and _BYTE limitations.
QB64 WIKI: Main Page

#### RhoSigma

• Jr. Member
• Posts: 75
• 4 * 1.0079 = 4.0026
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #4 on: March 01, 2013, 03:37:47 PM »
Quote from: Clippy on March 01, 2013, 01:06:29 PM
This is another functionality that was never finished in QB64....

Well, now you can finish it yourself, if you need it. BTW i had my concerns too, as Galleon was launching the GL branch without a really finished SDL branch. Personally i had at least finished the BETA state of the SDL branch
with some more bugfixes/completions and a version of at least 1.0 to reflect this.

Polygon functions: http://rhosigma-cw.net/down/GfxDemos.zip
Screen savers: http://rhosigma-cw.net/down/ScrSavers.zip
Libraries collection: http://rhosigma-cw.net/down/QB64Library.zip (extract into the QB64 folder, a new sub-folder "QB64Library" is created)

#### DSMan195276

• Hero Member
• Posts: 1986
• Yes
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #5 on: March 01, 2013, 09:49:28 PM »
Personally I like this, however this can't be added to QB64 since it breaks compatibility with QBasic. Since QBasic had VAL. If a QBasic program had a string that started with &B then it won't act the same. That's the entire reason &B doesn't work with VAL in the first place.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

#### Clippy

• Hero Member
• Posts: 16440
• I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #6 on: March 02, 2013, 02:26:51 AM »
If that were the case then why not say that &B is incompatible too? It SHOULD be added!

Code: [Select]
`PRINT VAL("&BO200")PRINT VAL("&")PRINT VAL("&H")`
All 3 PRINT 208. Yes that is a peculiarity of Qbasic, it doesn't like & at the beginning of VAL strings when H or O values don't come next. No error except the incorrect value. QB doesn't spaz out or crash.

QB64 returns 0 like it should.

We need a _BIN\$ function too. I started a contest to make one a few years back.

« Last Edit: March 02, 2013, 09:36:09 AM by Clippy »
QB64 WIKI: Main Page

#### codeguy

• Hero Member
• Posts: 3552
• what the h3ll did i name that code?
##### Re: Use VAL function with binary &B prefixed number strings
« Reply #7 on: March 02, 2013, 08:27:48 AM »
yes, we have that one too, but mine is generalized for base 2 to 36. it's easy enough to roll your own for that too.
and if ya NEED a specialize binary conversion routine: http://www.qb64.net/forum/index.php?topic=1080.msg7568#msg7568 (handles positive, negative, non-integer)
« Last Edit: March 02, 2013, 08:42:10 AM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html