Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BINARY FILE write
#1
hey guys... I understand how to read a hex file and get the data to ram, I am just asking how can i write the file. I have included my code for reading which works great... I just need to reverse it without adding spaces or anything leading at the front and end file... 

file is a ROM image so HEX "c000: 00 78 66 34 55 67 89" .eg no address or spaces. just 4k ascii dump I want to write back 
 
Code: (Select All)
DIM SHARED c16(7, 15, 255) AS _UNSIGNED _BYTE, charload AS _UNSIGNED _BYTE, CHARACTER64 AS STRING * 1
' ----------------------------------------------------------------------------------------------------
READROM816: OPEN FILENAME$ FOR BINARY AS #1
FOR CHRDOLLAR% = 0 TO 255
FOR VERTPIXEL% = 0 TO 15
GET #1, , charload
        FOR HORIPIXEL% = 0 TO 7
IF readbit(HORIPIXEL%, charload) THEN c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 1 ELSE c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 0
NEXT
NEXT
NEXT
CLOSE #1
RETURN

WRITEROM816: OPEN FILENAME$ FOR BINARY AS #1
FOR CHRDOLLAR% = 0 TO 255
HEXCHARACTER=0
FOR VERTPIXEL% = 0 TO 15
FOR HORIPIXEL% = 0 TO 7
CHARACTER% = c16(HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%)
NEXT HORIPIXEL%
??? PRINT OR PUT #1, HEX CODE HERE 'HEXCHARACTER "&H CHARACTER%"    EACH CELL IS ONE ASC(HEX 0 TO 255) SO WHEN WRITTEN IS "0A3D44FAFF" eg.
NEXT VERTPIXEL%
NEXT CHRDOLLAR%
CLOSE #1
RETURN
Do I use the 
                        put #1, with dot comma  at the end or not

    or

                         print #1 with dot comma at the end or not

also I know i need to change the above to send hex out to the file..

Thank you for any help or suggestions...
Reply
#2
It looked to me like there were some issues with what you were trying to do, so I tired to sort out the problems that looked like issues to me.  See if this isn't what you want:

Code: (Select All)
Dim Shared c16(7, 15, 255) As _Unsigned _Byte, charload As _Unsigned _Byte, CHARACTER64 As String * 1
' ----------------------------------------------------------------------------------------------------
READROM816: Open FILENAME$ For Binary As #1
For CHRDOLLAR% = 0 To 255
    For VERTPIXEL% = 0 To 15
        Get #1, , charload
        For HORIPIXEL% = 0 To 7
            If _ReadBit(charload, HORIPIXEL%) Then c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 1 Else c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 0
        Next
    Next
Next
Close #1
Return

WRITEROM816: Open FILENAME$ For Binary As #1
For CHRDOLLAR% = 0 To 255
    HEXCHARACTER = 0
    For VERTPIXEL% = 0 To 15
        For HORIPIXEL% = 0 To 7
            CHARACTER% = 0 'reset the variable
            If c16(HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) Then
                CHARACTER% = _SetBit(CHARACTER%, HORIPIXEL%) 'set the bits properly
            End If
        Next HORIPIXEL%
        Put #1, , CHARACTER%
    Next VERTPIXEL%
Next CHRDOLLAR%
Close #1
Return

It looks like the data is bitpacked into those bytes, which is what you were attempting to extract with the readbit in your code.  I *think* you probably had the variable and the position reversed, so I corrected that, unless I'm just seriously missing what's supposed to be going on here.

To print back to disk, you'd need to repack those bits into your byte first (the line with the _SetBit I added), and then you'd simply use PUT like above to put that byte back in position sequentially.

I *think* the above is what you're looking for, but without any test data and such to go by, all I can do is say, "It LOOKS like it's what you need, to me."  I don't see any obvious glitches now, it looks logical, and I'm thinking it will work -- but only time, compiling, and testing with actual data will determine if it does what YOU expect it to, or not.  Wink
Reply
#3
(08-24-2025, 07:35 AM)SMcNeill Wrote: It looked to me like there were some issues with what you were trying to do, so I tired to sort out the problems that looked like issues to me.  See if this isn't what you want:

Code: (Select All)
Dim Shared c16(7, 15, 255) As _Unsigned _Byte, charload As _Unsigned _Byte, CHARACTER64 As String * 1
' ----------------------------------------------------------------------------------------------------
READROM816: Open FILENAME$ For Binary As #1
For CHRDOLLAR% = 0 To 255
    For VERTPIXEL% = 0 To 15
        Get #1, , charload
        For HORIPIXEL% = 0 To 7
            If _ReadBit(charload, HORIPIXEL%) Then c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 1 Else c16(7 - HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 0
        Next
    Next
Next
Close #1
Return

WRITEROM816: Open FILENAME$ For Binary As #1
For CHRDOLLAR% = 0 To 255
    HEXCHARACTER = 0
    For VERTPIXEL% = 0 To 15
        For HORIPIXEL% = 0 To 7
            CHARACTER% = 0 'reset the variable
            If c16(HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) Then
                CHARACTER% = _SetBit(CHARACTER%, HORIPIXEL%) 'set the bits properly
            End If
        Next HORIPIXEL%
        Put #1, , CHARACTER%
    Next VERTPIXEL%
Next CHRDOLLAR%
Close #1
Return

It looks like the data is bitpacked into those bytes, which is what you were attempting to extract with the readbit in your code.  I *think* you probably had the variable and the position reversed, so I corrected that, unless I'm just seriously missing what's supposed to be going on here.

To print back to disk, you'd need to repack those bits into your byte first (the line with the _SetBit I added), and then you'd simply use PUT like above to put that byte back in position sequentially.

I *think* the above is what you're looking for, but without any test data and such to go by, all I can do is say, "It LOOKS like it's what you need, to me."  I don't see any obvious glitches now, it looks logical, and I'm thinking it will work -- but only time, compiling, and testing with actual data will determine if it does what YOU expect it to, or not.  Wink
hello, I posted the Ascii thingy im working on in programs in progress.. it has all the files in the zip... i am just trying to figure out how to write my data back into a straight binary.. I have got it working with the other file type.. just the binary to hex then write file..  my character is in 1,1,1,1,1,1,1,1 but i want to store as hex &hFF
Reply
#4
Code: (Select All)
_CONTROLCHR OFF
OPEN "FILENAME" FOR OUTPUT AS #1
FOR CHRDOLLAR% = 0 TO 255
    FOR VERTPIXEL% = 0 TO 15
        FOR HORIPIXEL% = 0 TO 7
' I TRIED TO FIGURE OUT TO THE POWER OF BUT DID NOT WORK  should be able to add chracter = chracter ^ horipixel%           
                IF c16(HORIPIXEL%, VERTPIXEL%, CHRDOLLAR%) = 1 THEN
                SELECT CASE HORIPIXEL%
                    CASE 7: chracter = chracter + 1
                    CASE 6: chracter = chracter + 2
                    CASE 5: chracter = chracter + 4
                    CASE 4: chracter = chracter + 8
                    CASE 3: chracter = chracter + 16
                    CASE 2: chracter = chracter + 32
                    CASE 1: chracter = chracter + 64
                    CASE 0: chracter = chracter + 128
                END SELECT
            END IF
        NEXT HORIPIXEL%
         PRINT CHR$(CHARACTER64);
                 PUT #1, , CHR$(CHARACTER64) '      THIS THE PROBLEM HERE... WANTING TO WRITE ASCII TO FILE TO READ LIKE ROM FILE (ROM
BIOS IMAGE)
    CHARACTER64=0
    NEXT VERTPIXEL%
NEXT CHRDOLLAR%
CLOSE #1
END
RETURN
Reply
#5
Define your type for starters:   DIM chracter AS _UNSIGNED _BYTE

I don't see why you don't just:  PUT #1, , chracter

What's the desire to convert it to a string character first with the CHR$ usage?
And why are you putting CHARACTER64 to the file, when you're changing the value of chracter in the SELECT CASE?

Your variable types aren't matching here.   And aren't your values in reverse order?  Bit 0 is normally the 2 ^ 0 bit, while bit 7 is the 2 ^ 7 bit.  Doesn't seem to me like you'd need a SELECT CASE here.  Just a simple:  chracter = character + 2 ^ HORIPIXEL%
Reply
#6
(08-24-2025, 05:18 PM)SMcNeill Wrote: Define your type for starters:   DIM chracter AS _UNSIGNED _BYTE

I don't see why you don't just:  PUT #1, , chracter

What's the desire to convert it to a string character first with the CHR$ usage?
And why are you putting CHARACTER64 to the file, when you're changing the value of chracter in the SELECT CASE?

Your variable types aren't matching here.   And aren't your values in reverse order?  Bit 0 is normally the 2 ^ 0 bit, while bit 7 is the 2 ^ 7 bit.  Doesn't seem to me like you'd need a SELECT CASE here.  Just a simple:  chracter = character + 2 ^ HORIPIXEL%
thank you heaps. i have forgot how to use the power.... smh and slap it Smile... the reverse is no issue... its an easy fix. when i put the data using put it is not the same for some strange reason.... most likely it is me (i know it is) I will change stuff anyway... ... the file does not duplicate original and corrupts on reading.  (needs to be ascii so I can write to eprom character rom")
Reply
#7
Why not write the entire c16 array with one PUT, and read it back in with one GET
as described in example #2 on  QB64PE doc for PUT ?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question fast file find with wildcards, optional date range, match # bytes, binary compare? madscijr 8 1,293 12-19-2024, 03:55 PM
Last Post: SpriggsySpriggs
  Binary file write eoredson 9 1,563 09-26-2023, 02:25 AM
Last Post: eoredson
  Write data to EXE file Steffan-68 8 1,977 05-15-2023, 06:41 PM
Last Post: Steffan-68

Forum Jump:


Users browsing this thread: 1 Guest(s)