Page 1 of 1

Hexadecimal Routines for QBasic

Posted: Tue Apr 22, 2008 5:13 pm
by dburley
Does anyone know where QBasic routines for working with hexadecimal numbers can be found. I need to add, subtract, and do two's compliment operations on strings of hexadecimal bytes.

Thanks in advance for any help you might give.

Dave

Posted: Wed Apr 23, 2008 11:35 am
by Valerie
Interesting question but I don't understand what you mean.

Hex arithmetic calculations are the same as decimal except that you must prefix the hex number with &H

So:

print &HC - &HB

will result in 1

as will

print 12 - 11

Also, what's a hexadecimal byte? A byte consists of 8 bits and the value contained in the byte can be expressed in binary, hexadecimal and/or decimal so that

binary 00001111 = decimal 15 = hexadecimal F

Two's complement is generally used in binary subtraction as shown here http://mathforum.org/library/drmath/view/55733.html

(Upper case is used for emphasis only, &HF produces the same as &hf)

& welcome to the QBCafe forums.

V..

Posted: Wed Apr 23, 2008 7:10 pm
by dburley
Thank You Valerie for your responce.
What my task consists of is to take a string like this:
:0C01E0000702250041052105A4040008C9
Which is from a hex file used to program a microprocessor, and change some of the bytes, figure out the checksum and write the line back to the hex file. The checksum is the last byte in the string and is the least significant byte of the two's compliment of the sum of all the the preceeding bytes.

Qbasic has many functions to work on strings, but I'm a little confused as to how to convert the bytes, like 0C in above line, to an array variable that later can be added with the + statement. Does the VAL function return 12 which would have to have &H added as a prefix before being stored then in a string array ?

Posted: Fri Apr 25, 2008 6:01 pm
by pebe
Although there is an instruction to change a Hex number into a string, I can’t find the reverse function listed. I think you will have to use a simple routine.

You need to go through the string, a byte at a time, using the mid$ instruction on consecutive pairs of digits to get numbers from 00 to FF.

Multiply the MSD by 16 (use a table for the conversion of A to F digits).
Then add the LSD. That will give a number 0-255. You can then file it in an array.

Then you can do a 2’s complement on consecutive numbers in the array. Here is a link to 2’s complement that may help.

http://bytes.com/forum/thread200909.html

Posted: Sat Apr 26, 2008 12:23 am
by buff1
'show ascii values of hex
st$="0C01E0000702250041052105A4040008C9"
for k&=1 to len(st$) step 2
char$=mid$(st$,k&,2)
value$="&H"+char$
print val(value$);" = ";char$
next
'basically anything below 32 is unprintable control characters
'however, if it is asm code you show perhaps look at it with debug

You can then do decimal addition subtraction etc then convert the result
back to hexadecimal using hex$

Posted: Thu Aug 07, 2008 10:59 am
by Dragoncat
I have just recently made a pair of programs that allows you to convert any data or file form Binary mode ascii to hexadecimal and back
(with no formating or spaces etc..) I have tested it fully and it works I don't know if this applies to the question at hand... if you are interested Email me at drgncat@humboldt1.com and I can send you a copy... you are free to use these programs but please make sure that I retain the copyright for these two programs and keep in mind that you can not modify either of these two programs and that the copyright notice must be left untouched... if you wish to integrate either of these two programs or make modifications please ask me first! thank you. the programs are called hex2bin.bas and bin2hex.bas
and I also have the Executable versions of these as well... (they would be zipped in a standard zipfile...)

Posted: Fri Oct 24, 2008 9:09 pm
by ZONOZ
This is slightly off topic I believe but it may be of interest to some one. I have created a program that reads the true values of any file (being program, graphical etc.) meaning the binary. And converts the values byte by byte into color representations ( 0 - FF : 256 ). Then displays it on screen. I created this to extract images from video games.