How to convert hexadecimal to integer

Discuss whatever you want here--both QB and non-QB related. Anything from the DEF INT command to the meaning of life!

Moderators: Pete, Mods

TRANERAECK
Coder
Posts: 29
Joined: Thu Mar 24, 2011 3:08 am

How to convert hexadecimal to integer

I need to know how to convert hexadecimal to integer. Here is my sample program:

Code: Select all

1 REM PROGRAM: COMPILER.BAS
2 REM TYPE OF COMPILER CONVERTING HEXADECIMAL
3 REM CREATED BY: GREGORY LYONS
4 REM CONTAINS ORIGINAL IDEAS BY GREGORY LYONS
5 REM PROGRAM IS FREE TO USE ALSO IS INCOMPLETE
20 INPUT "FILE TO MAKE AS TOKEN WITH -[.TKN]:", FILENAMETOKEN\$
30 IF INSTR(1, FILENAMETOKEN\$, ".") > 1 THEN PLACEOFEXTENSION = INSTR(1,

FILENAMETOKEN\$, ".")
40 IF INSTR(1, FILENAMETOKEN\$, ".") = 1 THEN PLACEOFEXTENSION = 1
50 LENGTHOFEXTENSION = LEN(FILENAMETOKEN\$) - PLACEOFEXTENSION
60 EXTENSION\$ = MID\$(FILENAMETOKEN\$, PLACEOFEXTENSION, LENGTHOFEXTENSION)
70 IF EXTENSION\$ <> "TKN" THEN EXTENSION\$ = ".TKN"
80 IF PLACEOFEXTENSION > 1 THEN FILENAME\$ = LEFT\$(FILENAMETOKEN\$,

(PLACEOFEXTENSION - 1))
90 IF PLACEOFEXTENSION = 1 THEN COLOR 4, 0: PRINT"ERROR #1 - STARTED FILE

WITH '.' OR NO EXTENSION ENTERED!!!": COLOR 7, 0: GOTO 20
100 IF PLACEOFEXTENSION = 0 THEN FILENAME\$ = FILENAMETOKEN\$
110 FILENAMETOKEN\$ = FILENAME\$ + EXTENSION\$
120 FILETOMAKE\$ = FILENAME\$ + ".EXE"
130 OPEN FILETOMAKE\$ FOR OUTPUT AS #1
140 FIRST\$ = "M" + "Z" + CHR\$(144)
150 PRINT #1, FIRST\$;
160 OPEN FILENAMETOKEN\$ FOR INPUT AS #2
170 WHILE NOT EOF(2)
180 SYSDB\$ = INPUT\$(2, 2)
190 CHEESY = CVI(SYSDB\$)
200 WELT\$ = HEX\$(CHEESY)
210 IF LEN(WELT\$) = 3 THEN WELT\$ = "0" + WELT\$
220 IF LEN(WELT\$) = 2 THEN WELT\$ = "0" + "0" + WELT\$
230 IF LEN(WELT\$) = 1 THEN WELT\$ = "0" + "0" + "0" + WELT\$
240 STUCK\$ = LEFT\$(WELT\$, 2)
250 SAMPLE\$ = MID\$(WELT\$, 2, 2)
260 FIRSTCHOICE\$ = LEFT\$(STUCK\$, 1)
270 SECONDCHOICE\$ = MID\$(STUCK\$, 2, 1)
280 THIRDCHOICE\$ = LEFT\$(SAMPLE\$, 1)
290 FOURTHCHOICE\$ = MID\$(SAMPLE\$, 2, 1)
300 FOR DIVERT = 1 TO 6
310 JUMBLE1\$ = FIRSTCHOICE\$
320 IF JUMBLE1\$ = CHR\$(64 + DIVERT) THEN FIRSTCHOICE\$ = STR\$(9 + DIVERT)
330 JUMBLE2\$ = SECONDCHOICE\$
340 IF JUMBLE2\$ = CHR\$(64 + DIVERT) THEN SECONDCHOICE\$ = STR\$(9 + DIVERT)
350 JUMBLE3\$ = THIRDCHOICE\$
360 IF JUMBLE3\$ = CHR\$(64 + DIVERT) THEN THIRDCHOICE\$ = STR\$(9 + DIVERT)
370 JUMBLE4\$ = FOURTHCHOICE\$
380 IF JUMBLE4\$ = CHR\$(64 + DIVERT) THEN FOURTHCHOICE\$ = STR\$(9 + DIVERT)
390 NEXT DIVERT
400 IF VAL(FIRSTCHOICE\$) = 0 THEN STUCK\$ = STR\$(VAL(SECONDCHOICE\$))
410 IF VAL(SECONDCHOICE\$) = 0 THEN STUCK\$ = STR\$(VAL(FIRSTCHOICE\$) * 10)
420 IF (VAL(SECONDCHOICE\$) > 0) AND VAL(FIRSTCHOICE\$) <0> 0) AND VAL(THIRDCHOICE\$) <> 0 THEN SAMPLE\$ =

STR\$(VAL(THIRDCHOICE\$) * VAL(FOURTHCHOICE\$))
460 STUCK\$ = STR\$(VAL(STUCK\$) - 1)
470 SYSDB\$ = CHR\$(ASC(STUCK\$))
480 PRINT #1, SYSDB\$;
490 SAMPLE\$ = STR\$(VAL(SAMPLE\$) - 1)
500 SYSDB\$ = CHR\$(ASC(SAMPLE\$))
510 PRINT #1, SYSDB\$;
520 WEND
530 CLOSE #2
540 CLOSE #1
550 END

A little bit of breaks in it due to length of line. I am currently stuck. Any help will be nice. burger2227
Veteran
Posts: 2355
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA
Qbasic converts hexadecimal values like &H60 to decimal automatically:

Code: Select all

decimal% = VAL("&H60")
PRINT decimal%

hexa\$ = "&H" + HEX\$(decimal%)
PRINT hexa\$
HEX\$ converts a decimal to hexadecimal without a leading space so it can be added to "&H" to create that representation.
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Kiyotewolf
Veteran
Posts: 96
Joined: Tue Apr 01, 2008 11:38 pm
It's good that he's trying to figure out a algo on his own to do it.
You can do it on paper with a calculator. Some people forget how.

~Kiyote!

I thought this was a mean joke program for a second by the variable name "WELTS."
Banana phone! We need more lemon pledge. * exploding fist of iced tea! * I see your psycho cat and counter with a duck that has a broken leg, in a cast.

TRANERAECK
Coder
Posts: 29
Joined: Thu Mar 24, 2011 3:08 am

I see what you are saying here

I was trying to figure out a compiler that doesn't rely on a function. Also, don't forget somewhere at the end of the loop to CLEAR the reading variable so the compiler won't use memory. I think if you use a library for extended memory that would be nice and also add all you can for commands with this listing I have a way to read two bytes which allows me to take the language I am working with which is QUICKBASIC using a library. I think when you make the library add + LIB's as much as you want for one big QLB. Kiyotewolf
Veteran
Posts: 96
Joined: Tue Apr 01, 2008 11:38 pm
That makes more sense now that you detailed "using it for a compiler without using internal functions."
I just took an exhaustive math course from the Electrical Union, and we covered Base 2, Base 16 numbers, I'll go out to the garage & grab my school notes & get them written down for you.

~Kiyote!
Banana phone! We need more lemon pledge. * exploding fist of iced tea! * I see your psycho cat and counter with a duck that has a broken leg, in a cast.

TRANERAECK
Coder
Posts: 29
Joined: Thu Mar 24, 2011 3:08 am

HAVE completed it.

It took sometime but I completed it. I was trying to convert a four digit HEX because it would be two ASCII numbers back to integer. I wanted to reproduce my compiler I had on my TANDY 1000. It was nice and it was in binary, I figured HEX would be simpler. I am not sure what else I did but the Registry on WINDOWS XP or any window os would stop the compilation from working. As we need to be able to run anything, not just BASIC or BASIC programs as DOS 2.10 was only capable of doing, since there was no other programming language in 1983. May have been, anyway this compiler used to work. I then started on more advanced techniques other posts. Oh well, here it is so anyone can see the way you convert a four digit HEX to 2 ASCII characters.

Code: Select all

REM PROGRAM: BASIC COMPILER
REM CREATED BY: GREGORY LYONS
CLEAR , , 28000

BEGIN:
INPUT "FILE TO COMPILE WITH-[.BAS]:", FILENAMEBASIC\$
IF INSTR(1, FILENAMEBASIC\$, ".") > 1 THEN
PLACEOFEXTENSION = INSTR(1, FILENAMEBASIC\$, ".")
END IF
IF INSTR(1, FILENAMEBASIC\$, ".") = 1 THEN PLACEOFEXTENSION = 1
LENGTHOFEXTENSION = LEN(FILENAMEBASIC\$) - PLACEOFEXTENSION
EXTENSION\$ = MID\$(FILENAMEBASIC\$, PLACEOFEXTENSION, LENGTHOFEXTENSION)
IF EXTENSION\$ <> "BAS" THEN EXTENSION\$ = ".BAS"
IF PLACEOFEXTENSION > 1 THEN
FILENAME\$ = LEFT\$(FILENAMEBASIC\$, (PLACEOFEXTENSION - 1))
END IF
IF PLACEOFEXTENSION = 1 THEN
COLOR 4, 0
PRINT "ERROR #1 - STARTED FILE WITH '.' OR NO EXTENSION ENTERED!!!"
COLOR 7, 0
GOTO BEGIN
END IF
IF PLACEOFEXTENSION = 0 THEN FILENAME\$ = FILENAMEBASIC\$
FILENAMEBASIC\$ = FILENAME\$ + EXTENSION\$
FILENAMEBASIC\$ = "C:\QBstuff\PROJECTS\COMPILER\" + FILENAMEBASIC\$
FILETOMAKE\$ = "C:\QBstuff\PROJECTS\" + FILENAME\$ + ".GRG"
OPEN FILETOMAKE\$ FOR OUTPUT AS #1
FIRST\$ = "M" + "Z" + CHR\$(144)
PRINT #1, FIRST\$;
OPEN FILENAMEBASIC\$ FOR INPUT AS #2
FOR FAIRVALUE = 1 TO 1000
IF NOT EOF(2) THEN SYSDB\$ = INPUT\$(1, 2)
IF NOT EOF(2) THEN SYSDB3\$ = INPUT\$(1, 2) ELSE SYSDB3\$ = " "
SYSDB\$ = SYSDB\$ + SYSDB3\$
CHEESY = CVI(SYSDB\$)
WELT\$ = HEX\$(CHEESY)
IF LEN(WELT\$) = 3 THEN WELT\$ = "0" + WELT\$
IF LEN(WELT\$) = 2 THEN WELT\$ = "0" + "0" + WELT\$
IF LEN(WELT\$) = 1 THEN WELT\$ = "0" + "0" + "0" + WELT\$
CONVERTVAL\$ = LEFT\$(WELT\$, 2)
SAMPLE\$ = MID\$(WELT\$, 2, 2)
FIRSTCHOICE\$ = LEFT\$(CONVERTVAL\$, 1)
SECONDCHOICE\$ = MID\$(CONVERTVAL\$, 2, 1)
THIRDCHOICE\$ = LEFT\$(SAMPLE\$, 1)
FOURTHCHOICE\$ = MID\$(SAMPLE\$, 2, 1)
FOR DIVERT = 1 TO 6
JUMBLE1\$ = FIRSTCHOICE\$
IF JUMBLE1\$ = CHR\$(64 + DIVERT) THEN FIRSTCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE2\$ = SECONDCHOICE\$
IF JUMBLE2\$ = CHR\$(64 + DIVERT) THEN SECONDCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE3\$ = THIRDCHOICE\$
IF JUMBLE3\$ = CHR\$(64 + DIVERT) THEN THIRDCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE4\$ = FOURTHCHOICE\$
IF JUMBLE4\$ = CHR\$(64 + DIVERT) THEN FOURTHCHOICE\$ = STR\$(9 + DIVERT)
NEXT DIVERT
IF VAL(FIRSTCHOICE\$) = 0 THEN CONVERTVAL\$ = STR\$(VAL(SECONDCHOICE\$))
IF VAL(SECONDCHOICE\$) = 0 THEN
CONVERTVAL\$ = STR\$((VAL(FIRSTCHOICE\$) * 10) + (VAL(FIRSTCHOICE\$) * 6))
END IF
IF (VAL(SECONDCHOICE\$) > 0) AND VAL(FIRSTCHOICE\$) <0> 0) AND VAL(THIRDCHOICE\$) <0> 1000 THEN VALUENOTDONE\$ = "TRUE"
OPEN "C:\QBstuff\PROJECTS\COMPILER\DORKO.DAT" FOR OUTPUT AS #3
PRINT #3, FILENAMEBASIC\$
PRINT #3, FILETOMAKE\$
PRINT #3, LOF(2)
PRINT #3, "1"
CLOSE #3
CLOSE #2
CLOSE #1
IF VALUENOTDONE\$ = "TRUE" THEN
GOTO KEILBAHSIE
ELSE
GOTO FINISH
END IF

KEILBAHSIE:
IF VALUENOTDONE\$ = "TRUE" THEN CLEAR , , 22000
OPEN "C:\QBstuff\PROJECTS\COMPILER\DORKO.DAT" FOR INPUT AS #3
LINE INPUT #3, FILENAMEBASIC\$
LINE INPUT #3, FILETOMAKE\$
INPUT #3, LENGTHOFCODE
INPUT #3, MULTIPLIER
CLOSE #3
OPEN FILETOMAKE\$ FOR APPEND AS #1
OPEN FILENAMEBASIC\$ FOR INPUT AS #2
FOR FAIRVALUE = 1 TO 1000
IF NOT EOF(2) THEN SYSDB\$ = INPUT\$(1, 2)
IF NOT EOF(2) THEN SYSDB3\$ = INPUT\$(1, 2) ELSE SYSDB3\$ = " "
SYSDB\$ = SYSDB\$ + SYSDB3\$
CHEESY = CVI(SYSDB\$)
WELT\$ = HEX\$(CHEESY)
IF LEN(WELT\$) = 3 THEN WELT\$ = "0" + WELT\$
IF LEN(WELT\$) = 2 THEN WELT\$ = "0" + "0" + WELT\$
IF LEN(WELT\$) = 1 THEN WELT\$ = "0" + "0" + "0" + WELT\$
CONVERTVAL\$ = LEFT\$(WELT\$, 2)
SAMPLE\$ = MID\$(WELT\$, 2, 2)
FIRSTCHOICE\$ = LEFT\$(CONVERTVAL\$, 1)
SECONDCHOICE\$ = MID\$(CONVERTVAL\$, 2, 1)
THIRDCHOICE\$ = LEFT\$(SAMPLE\$, 1)
FOURTHCHOICE\$ = MID\$(SAMPLE\$, 2, 1)
FOR DIVERT = 1 TO 6
JUMBLE1\$ = FIRSTCHOICE\$
IF JUMBLE1\$ = CHR\$(64 + DIVERT) THEN FIRSTCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE2\$ = SECONDCHOICE\$
IF JUMBLE2\$ = CHR\$(64 + DIVERT) THEN SECONDCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE3\$ = THIRDCHOICE\$
IF JUMBLE3\$ = CHR\$(64 + DIVERT) THEN THIRDCHOICE\$ = STR\$(9 + DIVERT)
JUMBLE4\$ = FOURTHCHOICE\$
IF JUMBLE4\$ = CHR\$(64 + DIVERT) THEN FOURTHCHOICE\$ = STR\$(9 + DIVERT)
NEXT DIVERT
IF VAL(FIRSTCHOICE\$) = 0 THEN CONVERTVAL\$ = STR\$(VAL(SECONDCHOICE\$))
IF VAL(SECONDCHOICE\$) = 0 THEN
CONVERTVAL\$ = STR\$((VAL(FIRSTCHOICE\$) * 10) + (VAL(FIRSTCHOICE\$) * 6))
END IF
IF (VAL(SECONDCHOICE\$) > 0) AND VAL(FIRSTCHOICE\$) <0> 0) AND VAL(THIRDCHOICE\$) <0> HOWMUCHDONE THEN VALUENOTDONE\$ = "TRUE"
KILL "C:\QBstuff\PROJECTS\COMPILER\DORKO.DAT"
OPEN "C:\QBstuff\PROJECTS\COMPILER\DORKO.DAT" FOR OUTPUT AS #3
PRINT #3, FILENAMEBASIC\$
PRINT #3, FILETOMAKE\$
PRINT #3, LOF(2)
PRINT #3, MULTIPLIER
CLOSE #3
CLOSE #2
CLOSE #1
IF VALUENOTDONE\$ = "TRUE" THEN
GOTO KEILBAHSIE
ELSE
GOTO FINISH
END IF

FINISH:
CLEAR , , 28000
PRINT FRE(-2)
KILL "C:\QBstuff\PROJECTS\COMPILER\DORKO.DAT"
END

I have to work on the PATHS so it goes in temporary files and can access any source code on the machine. So, when I kill the file it was temporary and not used. Also, the PATH for the source, I need to figure out. I am not sure what the FRE number should be, but the compilation won't use any memory for the variable I used to read the file with or the conversion variables. That, other posts, is what I am talking about the companies with the languages need to go back and recompile. It would mean there was more memory. BASIC is a good place to start because with this hunk of code I compiled another BASIC. It was much bigger with either another file for the library or I had a modern day compiler. Well, the stuff I don't remember means I have to try again. 