-stuff-
OPEN "File.z64" FOR BINARY (Or Random?) AS #1
PUT (Forget the rest of the command XD But I know it puts whatever a variable has in a certain spot)
CLOSE #1
z64 is a N64 ROM and Im making a text editor to insert code at the end of it.
Lee wrote:How can I insert hex in a file?
z64 is a N64 ROM and Im making a text editor to insert code at the end of it.
I'm not sure that a ROM is a file. You can easily append HEX (or any other) stuff at the end of a file. How it would get to a ROM I am not
clear. Never messed with it.
Menu - Last byte is currently 2007
1) Read hex
2) Write hex <------------ I chose this option
3) Finished
Character Number? 2008 <--- I selected last+1 to append new char
Value: e3 <---------------- I entered this Hex
Done
Menu - Last byte is currently 2008
1) Read hex <------------ I chose this option
2) Write hex
3) Finished
Character Number? 2008
Value is E3 <------------- I see the value was successfully added
OPEN "z64" 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
It might be better if you read some tutorial on this first... BTW, you use GET to get a character from a file, and PUT to put a character at some position. ASCII number go from 0 to 255, and they can represent text or a colour or whatever you want.
You get these ASCII characters in a string with length 1. Then you convert them to a numer 0-255 with ASC(asciicode$). You can make an ASCII character from a number with CHR$(number%).
I hope this will explain some things to you. Try messing around with it if you're not famaliar with it.
LOF(1) returns the length of an open file opened as #1. This returns the number of bytes or characters that are in the BINARY file. Thus you know the place marker for the next PUT is one extra byte.
TheChar is the one byte String I referred to as 0$ = SPACE$(1). Either way works fine. You only add (PUT) or read (GET) one byte at a time. That is because each ASCII character in the file is one byte. This also makes the file more compact than using 2 byte integers.
TheHex is a 2 byte string character in HEX form like AA or 1F. Also Integers are always 2 bytes returned by ASC(TheChar). HEX$ converts it to a HEX string number and UCASE$ makes the letters uppercase.
I simply don't have the time to tutor via forum questions. I provided a program that would do the required operation. I say just run the program and update the ROM