Ooops
May 31, 2020, 09:05:55 PM

Author Topic: How to cast from Int array to Byte array?  (Read 265 times)

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
How to cast from Int array to Byte array?
« on: May 02, 2020, 12:09:39 AM »
Hi guys, i've encountered a small hickup.Please see small code snipped below:
Code: [Select]
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

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: How to cast from Int array to Byte array?
« Reply #1 on: May 02, 2020, 01:04:06 AM »
This one throws the same errors....

Code: [Select]
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

Offline braxtonrivers

  • Jr. Member
  • **
  • Posts: 24
Re: How to cast from Int array to Byte array?
« Reply #2 on: May 02, 2020, 01:42:28 AM »
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

Offline braxtonrivers

  • Jr. Member
  • **
  • Posts: 24
Re: How to cast from Int array to Byte array?
« Reply #3 on: May 02, 2020, 02:10:11 AM »
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

Offline degac

  • Full Member
  • ***
  • Posts: 158
    • graphio.net
Re: How to cast from Int array to Byte array?
« Reply #4 on: May 02, 2020, 10:38:18 AM »
There's a little bug in your code, but it works

Code: [Select]
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




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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2975
Re: How to cast from Int array to Byte array?
« Reply #5 on: May 02, 2020, 11:49:54 AM »
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Framework Brl.StandardIO
  4.  
  5. Function StringToByteArray:Byte[](s:String)
  6.         Local result:Byte[] = New Byte[s.length]
  7.         Local i:Int
  8.         For Local b:Byte = EachIn s
  9.                 result[i] = b
  10.                 i :+ 1
  11.         Next
  12.         Return result
  13. End Function
  14.  
  15. Print StringToByteArray(Chr(50))[0] 'prints 50
  16. Print StringToByteArray(Chr(55))[0] 'prints 55
  17.  

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

Offline braxtonrivers

  • Jr. Member
  • **
  • Posts: 24
Re: How to cast from Int array to Byte array?
« Reply #6 on: May 02, 2020, 02:21:41 PM »
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

Offline Brucey

  • Full Member
  • ***
  • Posts: 127
Re: How to cast from Int array to Byte array?
« Reply #7 on: May 02, 2020, 06:59:22 PM »
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 :
Code: [Select]
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.

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: How to cast from Int array to Byte array?
« Reply #8 on: May 02, 2020, 09:06:16 PM »
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. ;)

Offline hackball

  • Jr. Member
  • **
  • Posts: 26
Re: How to cast from Int array to Byte array?
« Reply #9 on: May 03, 2020, 01:30:41 AM »
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.
Code: [Select]
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:
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.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal