Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opposite of HEX$ in Qbasic?
#1
I am making a BMP load/save program in QB, and I need a way to convert hexadecimal values from an opened file to decimal numbers. I know of the HEX$ function that can do the exact opposite, but there does not seem to be a similar function to that built in to QB. I'd rather not make my own hex>dec function if I can help it. Any suggestions?
img]http://b.domaindlx.com/cygh/mycophob3.gif[/img]
Reply
#2
VAL does that, just prefix the string with a "&H".. decnum% = val( "&H" + hexnum$ )
Reply
#3
I'm just dropping in here when the answer is already given (which I also knew).

But this seems kind of interesting: how about the reversal of OCT$, I've always coded that function myself and I couldn't find any function that reverses it. I think there isn't so such thing as &O right? Big Grin
Reply
#4
Quote:I'm just dropping in here when the answer is already given (which I also knew).

But this seems kind of interesting: how about the reversal of OCT$, I've always coded that function myself and I couldn't find any function that reverses it. I think there isn't so such thing as &O right? Big Grin

D$="10":O=VAL("&O"+D$):CLS:?O

It works for me Smile
Reply
#5
Wow what do you know. I never knew that existed.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#6
Nemesis:
Wrong thread, you should be posting to the obfuscated code challenge Big Grin
That CLS just before the end is perfect!!!
Antoni
Reply
#7
I didn't know there was an obfuscated qbasic code contest... hmmm, maybe i should make one... anyways, here's code to convert back and forth between octal or hex. Note that octal and hex numbers are stored as strings:

Code:
'For some reason I decided to put the octal first, don't ask why...
octalnum$ = OCT$(decimalnum%)
octalnum$ = "&O" + octalnum$
decimalnum% = VAL(octalnum$)
'Here's the hexadecimal...
hexnum$ = HEX$(decimalnum%)
hexnum$ = "&H" + hexnum$
decimalnum% = VAL(hexnum$)
'And here's a bit of code to convert your own hexadecimal number:
hexnum$ = "&HFF"
decimalnum% = VAL(hexnum$)
'decimalnum% should now be equal to 255. You should be able to
'test this by PRINTing the numbers, like this:
PRINT "Hex num: ";
PRINT hexnum$
PRINT "Decimal Equivalent: ";
PRINT STR$(decimalnum%)

I used this kind of code to write a hexadecimal/octal calculator once. Those are useful. It even PRINTed the associated ASCII character too! Note: For un-named hex or octal constants in the place of a decimal number, the quotation marks are not required. For example, you can type:

Code:
'Notice no quotation marks surrounding the &H14:
thenum = &H14 - 1

What we really need is some sort of function that can convert a number from any base to any base. It would look like this:

FUNCTION BASECONV$(basefrom, baseto, numfrom$)

where basefrom would be the base which numfrom$ is represented in, any number from 2 (binary) to 36. 16 would be hexadecimal, 8 octal, and 10 decimal. The same for baseto. The value returned would be a string that could be converted back by calling the function again. Like this:

base18$ = BASECONV$(10, 18, "12")
base10$ = BASECONV$(18, 10, base18$)

Here are the bases:

Code:
2 (Binary)
01
3
012
4
0123
5
01234
6
012345
7
0123456
8 (Octal)
01234567
9
012345678
10 (Decimal)
0123456789
11
0123456789A
12
0123456789AB
13
0123456789ABC
14
0123456789ABCD
15
0123456789ABCDE
16 (Hexadecimal)
0123456789ABCDEF
...
all the way to base
36
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ

Good luck! 8)
rogramming is like life - you work first, then play. :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce:
Reply
#8
There's one at QBNZ somewhere...
Reply
#9
Wonder where Octal and Hex come from?

Before the IBM 360 came out about 1963, everything was BCD and Octal.

BCD (Binary Coded Decimal) was generally 6 bits, although some called the 4 bit version for representing 0 to 9, BCD also. This was used on character oriented machines. Some older character machines used codes like "2 out of 5", where only 2 of the 5 bits could be on at any given time.

OCTAL was used on the binary (not character oriented) machines. Octal was very popular. You could take a string of bits, usually the size of the machine's registers, and print out the registers and memory locations grouping the bits (usually from right to left) 3 at a time, giving values from 0 to 7, that is, to the base 8. "Octo" in Greek means eight. Some programmers got real good at adding and subtracting Octal values.

BYTE-OCTAL also known as 3-3-2:
When codes went to 8 bits, ASCII and EBCDIC, just before the IBM 360 came out, programmers represented the eight bits in Byte-Octal, due to their Octal experience. However, 3 doesn't go evenly into 8, so they just did not include the extra bit and used 3 bits, 3 bits, and 2 bits. Depending on the meaning of the byte, the extra bit was either not included in front or back.

HEX or HEXADECIMAL NOTATION came out with the IBM 360. It was the same 8-bit byte as Byte Octal but grouped 4 bits as a time, with no bit allignment problems. The problem with this base 16 code is that single position values go from 0 to F, where A thru F covers 10 thru 15. Not as easy as Octal to add/subt by hand.
*****
Reply
#10
How about this? Big Grin
[syntax="qbasic"]
'Omni-base converter
'Peter O'Rourke (Marinedalek) 2004
DECLARE FUNCTION reverse$ (text$)
DECLARE FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)
DIM SHARED digitarray AS STRING * 36
digitarray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CLS
PRINT baseconv$(16, 2, "FFFF")

FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)
IF baseto% <= 1 OR basefrom% < 1 OR baseto% > 36 OR basefrom% > 36 THEN baseconv$ = "0": EXIT FUNCTION
FOR position% = LEN(numfrom$) TO 1 STEP -1
tempdec& = tempdec& + (basefrom% ^ (LEN(numfrom$) - position%)) * (INSTR(digitarray, MID$(numfrom$, position%, 1)) - 1)
NEXT position%
IF baseto% = 10 THEN
baseconv$ = LTRIM$(STR$(tempdec&)) 'skip last calculation if destination is
'decimal - it 'd be a waste of time.
ELSE
DO
tempdest$ = MID$(digitarray, (tempdec& MOD baseto%) + 1, 1) + tempdest$
tempdec& = tempdec& \ baseto%
LOOP UNTIL tempdec& = 0
baseconv$ = tempdest$
END IF
END FUNCTION

FUNCTION reverse$ (text$)
FOR i% = LEN(text$) TO 1 STEP -1
temptxt$ = temptxt$ + MID$(text$, i%, 1)
NEXT i%
reverse$ = temptxt$
END FUNCTION

[/syntax]

Any feedback?
8% of the teenage population smokes or has smoked pot. If you're one of the 2% who hasn't, copy and paste this in your signature.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)