How to make StringByteLengthand Copymemory with SB?

Just starting out? Need help? Post your questions and find answers here.
kingwolf71
Posts: 21
Joined: Wed May 01, 2019 10:14 am

How to make StringByteLengthand Copymemory with SB?

Post by kingwolf71 »

I am doing some cross platform coding, and I am getting stuck with these 2 functions

StringByteLength()
CopyMemory()

How can I replicate these 2 functons with SpiderBasic?
Fred
Site Admin
Posts: 1510
Joined: Mon Feb 24, 2014 10:51 am

Re: How to make StringByteLengthand Copymemory with SB?

Post by Fred »

Both should be doable with Peek/Poke combination, could you post your sample code to see if it can be done otherwise ?
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to make StringByteLengthand Copymemory with SB?

Post by Peter »

StringByteLength(): Perhaps this one?

Code: Select all

Procedure StringByteLength(String$, Format = #PB_Unicode)
  
  Protected sbl
  
  Select Format
    Case #PB_Ascii
      sbl = Len(String$)
    Case #PB_UTF8, #PB_Unicode
      ! v_sbl = new Blob([v_string$]).size;
  EndSelect
  
  ProcedureReturn sbl
  
EndProcedure

Debug StringByteLength("a", #PB_Ascii)
Debug StringByteLength("a", #PB_UTF8)
Debug StringByteLength("a", #PB_Unicode)

Debug StringByteLength("ä", #PB_Ascii)
Debug StringByteLength("ä", #PB_UTF8)
Debug StringByteLength("ä", #PB_Unicode)
kingwolf71
Posts: 21
Joined: Wed May 01, 2019 10:14 am

Re: How to make StringByteLengthand Copymemory with SB?

Post by kingwolf71 »

Code: Select all


 Structure _membersMemoryFileClass
      eof.w
      flags.w
      fileBase.i
      fileSize.i
      initialSize.i
      initialPageSize.i
      usedFile.i    ;Lof.
      filePointer.i ;Loc.
EndStructure

Global Dim           *Handle._membersMemoryFileClass(1)

memPTR() = *handle(0)\FilePointer;
       
            CompilerIf Defined(SB_Compiler_SpiderBasic, #PB_Constant)
               For i = 0 To lengthToRead - 1
                  PokeB( memoryBuffer, i, PeekB(memPTR(), i + memPTR()\filePointer) )
               Next
            CompilerElse
               CopyMemory(memPTR()\FileBase + memPTR()\filePointer, memoryBuffer, lengthToRead)   
            CompilerEndIf
this is the code I tried which doesn't actually work (in spiderbasic)
kingwolf71
Posts: 21
Joined: Wed May 01, 2019 10:14 am

Re: How to make StringByteLengthand Copymemory with SB?

Post by kingwolf71 »

Peter wrote:StringByteLength(): Perhaps this one?

Code: Select all

Procedure StringByteLength(String$, Format = #PB_Unicode)
  
  Protected sbl
  
  Select Format
    Case #PB_Ascii
      sbl = Len(String$)
    Case #PB_UTF8, #PB_Unicode
      ! v_sbl = new Blob([v_string$]).size;
  EndSelect
  
  ProcedureReturn sbl
  
EndProcedure

Debug StringByteLength("a", #PB_Ascii)
Debug StringByteLength("a", #PB_UTF8)
Debug StringByteLength("a", #PB_Unicode)

Debug StringByteLength("ä", #PB_Ascii)
Debug StringByteLength("ä", #PB_UTF8)
Debug StringByteLength("ä", #PB_Unicode)

Thats pretty cool, thanks
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: How to make StringByteLengthand Copymemory with SB?

Post by Peter »

kingwolf71 wrote:Thats pretty cool, thanks
Note that the procedure has not been sufficiently tested. It may therefore return incorrect values.
Post Reply