Change a byte in memory

Just starting out? Need help? Post your questions and find answers here.
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Change a byte in memory

Post by SPH »

Code: Select all

UseJPEGImageDecoder()

file$ = OpenFileRequester("Sélectionnez un fichier","","JPG (.jpg)|*.jpg|All files (*.*)|*.*",0)
  If file$
    If ReadFile(0, file$) 
      length = Lof(0)                            ; Lit la taille en octets du fichier 
      *MemoryID = AllocateMemory(length)         ; alloue un bloc mémoire de la taille du fichier
      If *MemoryID
        bytes = ReadData(0, *MemoryID, length)   ; Lit les données du fichier et les place dans le bloc mémoire
      EndIf
      CloseFile(0)
    EndIf
  EndIf
  
  Resultat = CatchImage(0, *MemoryID, length)
 
I want to delete the first byte on the memoryID
What is the instruction ?

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
Peyman
Enthusiast
Enthusiast
Posts: 203
Joined: Mon Dec 24, 2007 4:15 pm
Location: Iran

Re: Change a byte in memory

Post by Peyman »

you can use pokeB to set the first byte null :

Code: Select all

PokeB(*MemoryID, 0)
Sorry for my bad english.
User avatar
STARGÅTE
Addict
Addict
Posts: 2226
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Change a byte in memory

Post by STARGÅTE »

Or you can use a Byte-Array like:

Code: Select all

Structure ByteArray
	b.b[0]
EndStructure

Define Text.s = "Some Text"
Define *MemoryID.ByteArray = @Text

; change a byte 
*MemoryID\b[4] = '!'

Debug Text
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Change a byte in memory

Post by tinman »

Even if you set the first byte to null you'd have to change the address you use in the call to CatchImage() to be "*MemoryID + 1" and reduce length by 1, otherwise it would not be a correctly formatted JPEG image in memory.

Why not just read only what you need?

Code: Select all

UseJPEGImageDecoder()

file$ = OpenFileRequester("Sélectionnez un fichier","","JPG (.jpg)|*.jpg|All files (*.*)|*.*",0)
  If file$
    If ReadFile(0, file$)
      length = Lof(0) - 1                            ; Lit la taille en octets du fichier
      *MemoryID = AllocateMemory(length)         ; alloue un bloc mémoire de la taille du fichier
      If *MemoryID
        FileSeek(0, 1)
        bytes = ReadData(0, *MemoryID, length)   ; Lit les données du fichier et les place dans le bloc mémoire
      EndIf
      CloseFile(0)
    EndIf
  EndIf
 
  Resultat = CatchImage(0, *MemoryID, length)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
User avatar
SPH
Enthusiast
Enthusiast
Posts: 561
Joined: Tue Jan 04, 2011 6:21 pm

Re: Change a byte in memory

Post by SPH »

Ho, thx :o

!i!i!i!i!i!i!i!i!i!
!i!i!i!i!i!i!
!i!i!i!
//// Informations ////
Portable LENOVO ideapad 110-17ACL 64 bits
Version de PB : 6.12LTS - 64 bits
epidemicz
User
User
Posts: 86
Joined: Thu Jan 22, 2009 8:05 am
Location: USA
Contact:

Re: Change a byte in memory

Post by epidemicz »

STARGÅTE wrote:Or you can use a Byte-Array like:

Code: Select all

Structure ByteArray
	b.b[0]
EndStructure

Define Text.s = "Some Text"
Define *MemoryID.ByteArray = @Text

; change a byte 
*MemoryID\b[4] = '!'

Debug Text
Wow, that's a great example, much more elegant than what would have done :/

Code: Select all

Define Text.s = "Some Text"
Define *MemoryID = @Text

; change a byte
PokeC(*MemoryID+4, Asc("!"))

Debug Text
Image
Post Reply