Inserting Hexadecimal in a file

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Inserting Hexadecimal in a file

Post by Sinuvoid »

Hey peeps! Im back with more questions XD Arn't yeahs happy :P

Anyways, heres my problem, How can I insert hex in a file? Like anywhere? Here's what I think it may look like:

Code: Select all


-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.
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

You want to open a binary file (if that's not the problem, please clarify it a bit, because I don't understand the problem)?

That goes like this:

OPEN file$ FOR BINARY AS #1 (Suppose file #1 is free)
FOR position = 1 to 10
PUT #1, position, number
NEXT position
CLOSE #1

There may be some things wrong, but I think most of this is right. Number is 0-255.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: Inserting Hexadecimal in a file

Post by Mac »

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.

Mac
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

A ROM is a file full of data. People edit it using hex editors, and yes I want to appended stuff to the end of it in Hex , but dont know how :?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Hmmmmmmm same problem asking questions

Post by burger2227 »

You better learn how to ask a question here kiddo!

I don't decipher lame questions! And you have been warned BEFORE.

So I WILL PASS!

Mr. Obvious
Please acknowledge and thank members who answer your questions!
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
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

OK. Here is a more indepth perspective of my question.

Here's what Hex looks like folks:

Code: Select all

This is hex, translates tooooooooo-----------------V
1A F4 D7 B1 F5 AA 22 ------------> Some random ASCII characters.
Now, imagine a whole file of that. What I want to do is put hex at the end of the file. Like so:

Code: Select all


-Hex code-
*Insert Your hex code here, at the end of file*

Get it?!?!
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Why HEX? Any ASCII code could work from 0 up to 255. Hex can go beyond that limit easily.

Just use: PUT #1, , CHR$(Number) to append it to a BINARY file end.

To read the numbers, use GET #1, bytenumber%, O$ , where O$ = SPACE$(1) and use ASC(O$) if needed. Use a loop to read a file.

If you ever need a HEXadecimal value returned just use:

HexNum$ = "&H" + HEX$(DecimalNumber)

To convert it to a QB value you must add "&H" to the beginning of the value. VAL will convert it back to decimal where needed.

Ted
Please acknowledge and thank members who answer your questions!
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
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac »

Lee wrote: Get it?!?!
Got it.

Made a demo file, "z64". The demo program produced

Code: Select all

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
Mac

Code: Select all

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
Sinuvoid
Veteran
Posts: 155
Joined: Wed Jul 25, 2007 8:20 am

Post by Sinuvoid »

MAC I LOVE YOU. That is EXACTLY what I needed, but you also went beyond what was asked! Anyways, could you please explain the code?

The lines that confuse me are:

DIM Size AS LONG: Size = LOF(1)

DIM TheChar AS STRING * 1 'Why times by 1?
DIM TheHex AS STRING * 2 'Why times by 2?

GET #1, CharNum, TheChar 'Reading hex. How did we already clarify what TheChar is?

TheHex = UCASE$(HEX$(ASC(TheChar))) 'This really confuses me.


'??? XD, confusing too
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)

Size = LOF(1) 'Still yet to figure out what this does.

Anyways, BIG thanks mac! You too burger!
User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss »

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.
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

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.

Ted
Please acknowledge and thank members who answer your questions!
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
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Thanks

Post by Mac »

Thanks, guys, for the explanations.

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

Mac
Post Reply