Wednesday, January 4, 2012

A Simple Hex Editor - Qbasic / FreeBASIC / QB64


This is A Simple Hex Editor I found on the net . This Editor reads one Character at a time (byte per byte),This method is really slow, specially with big files and not that efficient but still good for beginners in understanding the idea and concept

Source code can be found below :-

INPUT "Enter BINARY File's Name -->", Name$
OPEN Name$ FOR BINARY AS #1
DIM Size AS LONG: Size = LOF(1)
DIM CharNum AS LONG
CLS
DO
PRINT : PRINT "Menu - Last byte is currently"; Size
PRINT " 1) Read hex"
PRINT " 2) Write hex"
PRINT " 3) Finished"
SELECT CASE INPUT$(1)
CASE "1": GOSUB Read1
CASE "2": GOSUB Write1
CASE "3": EXIT DO
CASE ELSE: PRINT "GOOF use 1 2 3 only"
END SELECT
LOOP
CLOSE #1
SYSTEM

DIM TheChar AS STRING * 1
DIM TheHex AS STRING * 2


Read1:
INPUT "Character Number"; CharNum
IF CharNum < 1 THEN PRINT "Too little": RETURN
IF CharNum > Size THEN PRINT "Too big": RETURN
GET #1, CharNum, TheChar
TheHex = UCASE$(HEX$(ASC(TheChar)))
PRINT "Value is "; TheHex
RETURN


Write1:
INPUT "Character Number"; CharNum
IF CharNum < 1 THEN PRINT "Too little": RETURN

IF CharNum > Size + 1 THEN PRINT "Too big": RETURN
LINE INPUT "Value: "; TheHex
TheHex = UCASE$(TheHex)
IF LEN(TheHex) <> 2 THEN PRINT "Need two chars such as 3F": RETURN
DIM w1 AS INTEGER, W2 AS INTEGER
w1 = INSTR("0123456789ABCDEF", LEFT$(TheHex, 1))
IF w1 = 0 THEN PRINT "Use 0-F only": RETURN
W2 = (w1 - 1) * 16
w1 = INSTR("0123456789ABCDEF", RIGHT$(TheHex, 1))
IF w1 = 0 THEN PRINT "Use 0-F only": RETURN
W2 = W2 + w1 - 1
TheChar = CHR$(W2)
PUT #1, CharNum, TheChar
Size = LOF(1)
PRINT "Done"
RETURN

No comments: