How to cast from Int array to Byte array?

Started by hackball, May 02, 2020, 00:09:39

Previous topic - Next topic

hackball

Hi guys, i've encountered a small hickup.Please see small code snipped below:
Type ilbmFile   
    Global formChunk:Byte[4]
    Global formLength:Int
    Global ilbmHead:Byte[4]
   
    Method IsFORM()
        If Self
            If Self.formChunk[0..3] = [$46, $4F, $52, $4D]    Then Return True    '["F", "O", "R", "M"]     [46, 4F, 52, 4D]
           
        EndIf
    End Method

Is there a smart solution to do this? I am getting errors for not a string array, cant convert Int array to byte array ...
Please help!
(i know it is probably a stupid question.)BTW using BMax 1.5

hackball

This one throws the same errors....

Method BytesToArray:Byte[] (a:String, b:String, c:String, d:String)
       
        Local narray:Byte[] = [Asc(a), Asc(b), Asc(c), Asc(d)]
       
        Return narray
       
    End Method

braxtonrivers

Hi,

Hope this helps

Thanks
Braxton

SuperStrict

Framework brl.standardio

Local b:Byte[]=BytesToArray("F","O","R","M")

For Local i:Int=0 Until b.length
   Print Chr(b)
Next

Function BytesToArray:Byte[] (a:String, b:String, c:String, d:String)
   Local narray:Byte[] = [Byte(Asc(a)), Byte(Asc(b)), Byte(Asc(c)), Byte(Asc(d))]
  Return narray
End Function

braxtonrivers

#3
Hi,

Hope this helps as well

Thanks
Braxton

SuperStrict

Framework brl.standardio

Local chunk_header_to_test:String="FORM"

Local result:String[]=["False","True"]

Print result[isForm(chunk_header_to_test[0..4],MakeChunk($46, $4F, $52, $4D))]
Print result[isForm(chunk_header_to_test[0..4],"FARM")]

Function isForm:Int(chunk_in:String,check_header:String)
   If chunk_in=check_header Then Return True
   Return False
End Function

Function MakeChunk:String(a:Int, b:Int, c:Int, d:Int)
  Return Chr(Byte(a))+Chr(Byte(b))+Chr(Byte(c))+Chr(Byte(d))
End Function

degac

There's a little bug in your code, but it works


SuperStrict

Framework brl.standardio

Local b:Byte[]=BytesToArray("F","O","R","M")

For Local i:Byte=EachIn b
Print i
Next


For Local i:Int=0 Until Len(b)
Print Chr(b[i])
Next

Function BytesToArray:Byte[] (a:String, b:String, c:String, d:String)
   Local narray:Byte[] = [Byte(Asc(a)), Byte(Asc(b)), Byte(Asc(c)), Byte(Asc(d))]
  Return narray
End Function





Quote from: braxtonrivers on May 02, 2020, 01:42:28
Hi,

Hope this helps

Thanks
Braxton

SuperStrict

Framework brl.standardio

Local b:Byte[]=BytesToArray("F","O","R","M")

For Local i:Int=0 Until b.length
   Print Chr(b)
Next

Function BytesToArray:Byte[] (a:String, b:String, c:String, d:String)
   Local narray:Byte[] = [Byte(Asc(a)), Byte(Asc(b)), Byte(Asc(c)), Byte(Asc(d))]
  Return narray
End Function
If there's a problem, there's at least one solution.
www.blitzmax.org

Derron

Code (Blitzmax) Select

SuperStrict

Framework Brl.StandardIO

Function StringToByteArray:Byte[](s:String)
Local result:Byte[] = New Byte[s.length]
Local i:Int
For Local b:Byte = EachIn s
result[i] = b
i :+ 1
Next
Return result
End Function

Print StringToByteArray(Chr(50))[0] 'prints 50
Print StringToByteArray(Chr(55))[0] 'prints 55


And one could maybe convert the string via "s.ToCString()" and cast this byte pointer then to a byte array or so ...


Regarding the original question:
You might also encounter issues like this:
local doubleArray:double[] = [1.2, 1.3] '<--- failing
as you need to manually define the type of the numeric:
local doubleArray:double[] = [1.2:double, 1.3:double] '<--- working


bye
Ron

braxtonrivers

#6
Hi Derron,

Thank you for noticing that, I completely missed that earlier, for some reason when i pasted it into the message it ate the [ i ] thinking it was a tag

Thanks
Braxton

Brucey

Interestingly, I've been writing a PNG parser (in Java) this week, which is has a form similar to IFF files, insofar as it is divided into chunks with 4 character names.

To do a fourcc test, you might do something like :

SuperStrict

Framework Brl.Standardio

Local id:String = "FORM"
Local bytes:Byte[] = [$46:Byte, $4F:Byte, $52:Byte, $4D:Byte]

Print IsChunkID(id, bytes)

bytes = [$45:Byte, $4F:Byte, $52:Byte, $4D:Byte]

Print IsChunkID(id, bytes)

Function IsChunkID:Int(id:String, bytes:Byte[])
For Local i:Int = 0 Until id.Length
If i = bytes.Length Then
Return False
End If

If id[i] <> bytes[i] Then
Return False
End If
Next
Return True
End Function


Remember that IFFs are Big Endian, so you'll need to interpret non-bytes in the correct way.

hackball

Thank you all! This is very helpful. Indeed PNG format design is heavily borrowing from EAs Interchangable File Format (IFF). So that is not a coincidence, just smart choice. ;)

hackball

#9
Just a quick followup:
everything seems to work now, even with my stupid way of doing things in BMax. ::)
I keep having troubles with endianess; i can see the files properly in a hex editor but once loaded into a Bank all is f***d up.
Solved it, anyway.Method ReadEndianShort:Short (buf:TBank, offset)
        Return buf.PeekByte(offset) Shl 8 + buf.PeekByte(offset+1)
    End Method
   
    Method GetBMHD (buf:TBank)
        'chunk starts with BMHD at byte 12
        'then follows chunk length Int
        'then from byte 20 the struct starts
        Local bufadr:Byte Ptr = buf.Lock ()
       
        If Self
            If bmhd
                'Print "bmhd struct:"
                'MemCopy (Byte Ptr(Self.bmhd), bufadr + 20, SizeOf(ilbmBMHD))   'doesn't work, Endian??
                bmhd.w                = ReadEndianShort (buf, 20)
                bmhd.h                = ReadEndianShort (buf, 22)
                bmhd.x                = ReadEndianShort (buf, 24)
                bmhd.y                = ReadEndianShort (buf, 26)
                bmhd.nPlanes        = buf.PeekByte(28)
                bmhd.compression    = buf.PeekByte(30)
            EndIf
        EndIf
       
        buf.Unlock ()
    End Method

Edit:
Quote from: Brucey on May 02, 2020, 18:59:22
Remember that IFFs are Big Endian, so you'll need to interpret non-bytes in the correct way.
Yes! Thanks for the reminder. I keep encountering those problems for ADFs and stuff as well.