freebasic

How to break an integer into single bytes and merge them again in FreeBASIC?


I'm making a simple steganography program to hide data in PNG files. Decoding/encoding single bytes was easy, but I also need to hide a header in the PNG file. This header will contain the filesize in bytes to know exactly how many bytes I need to extract the file (too many bytes and tge extracted file will be corrupted).

So I need to break the integer into single bytes (since integers in FB is 32 bits wide, this will result in four separate bytes). Then these bytes will be encoded into the first 16 pixels of the PNG image (in my steganography algorithm 1 decoded byte = 4 encoded and I use only R and B values for data storage). How might I do this?

TL;DR: I need to know how to break integers into four individual bytes and then merge those bytes into integers again.


Solution

  • My friend on FB forums, Mysoft created this example how to do that. Also, thanks for responsiveness and quick answer stackoverflow's community.

    dim as integer x = &hFF88442211, y
    dim as integer b1,b2,b3,b4
    
    b1 =  x and &hFF
    b2 = (x shr 8) and &hFF
    b3 = (x shr 16) and &hFF
    b4 = (x shr 24) and &hFF
    
    y = b1+(b2 shl 8)+(b3 shl 16)+(b4 shl 24)
    
    print hex$(x),hex$(y)
    print hex$(b1),hex$(b2),hex$(b3),hex$(b4)