Like mid Freebasic function for values

DOS specific questions.
Post Reply
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Like mid Freebasic function for values

Post by Cpcdos »

Hi, It's me again

I would like to find a FreeBasic function in manual for optimise this :

Code: Select all

Print "    A:" & hex(ASC(Mid(*Data, 1, 1)), 2) & "."
Print "    R:" & hex(ASC(Mid(*Data, 2, 1)), 2) & "."
Print "    G:" & hex(ASC(Mid(*Data, 3, 1)), 2) & "."
Print "    B:" & hex(ASC(Mid(*Data, 4, 1)), 2) & "."
Data is a ZString PTR type that I can convert to Unsigned long. It contents 0xAABBCCDD
Results :
A:AA
R:BB
G:CC
B:DD
He display hexa values, I must cut Data in 4 parts, and I'm upset to pass by string mid() function..
There exist function for cut "from-to" unsigned long/unsigned integer values ?

Thank you :)
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Like mid Freebasic function for values

Post by sancho2 »

Hello:
I am not sure this is what you are after:

Code: Select all

Dim sss As ZString * 8 = Chr(&haa) + Chr(&hbb) + Chr(&hcc) + Chr(&hdd)
Dim zData As ZString Ptr = @sss

Dim As ULong ptr n     ' 

Print "    A:" & hex(ASC(Mid(*zData, 1, 1)), 2) & "."
Print "    R:" & hex(ASC(Mid(*zData, 2, 1)), 2) & "."
Print "    G:" & hex(ASC(Mid(*zData, 3, 1)), 2) & "."
Print "    B:" & hex(ASC(Mid(*zData, 4, 1)), 2) & "."

n = StrPtr(*zData)


Print Hex(LoByte(LoWord(*n)))
Print Hex(HiByte(LoWord(*n)))
Print hex(LoByte(HiWord(*n)))
Print Hex(HiByte(HiWord(*n)))
Print
 
sleep
You can also bypass the Mid function using the [] as in:

Code: Select all

Print Hex(Asc(zData[0]))
Cpcdos
Posts: 207
Joined: Mar 06, 2013 13:52
Location: France - LYON 69003
Contact:

Re: Like mid Freebasic function for values

Post by Cpcdos »

Oh perfect

Code: Select all

Print Hex(Asc(zData[0]))
is the best solution for me!
Now I use :

Code: Select all

Print "    A:" & Hex(Asc(Data[0])) & "."
Print "    R:" & Hex(Asc(Data[1])) & "."
Print "    V:" & Hex(Asc(Data[2])) & "."
Print "    B:" & Hex(Asc(Data[3])) & "."
Thank you sancho2 :)

nb: If someone have other method, do not hesitate.

Regards
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Like mid Freebasic function for values

Post by fxm »

Code: Select all

Type Zstring_Ubytes
  Union
    z As Zstring * 5
    Type
      ub0 As Ubyte
      ub1 As Ubyte
      ub2 As Ubyte
      ub3 As Ubyte
    End Type
  End Union
End Type

Dim z0 As Zstring * 5 = Chr(&haa) + Chr(&hbb) + Chr(&hcc) + Chr(&hdd)

'--------------------------------------------------------------------

Dim As Zstring_Ubytes Ptr plb = Cast(Any Ptr, @z0)

Print Hex(plb->ub0)
Print Hex(plb->ub1)
Print Hex(plb->ub2)
Print Hex(plb->ub3)

Sleep
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Like mid Freebasic function for values

Post by fxm »

Cpcdos wrote:Now I use :

Code: Select all

Print "    A:" & Hex(Asc(Data[0])) & "."
Print "    R:" & Hex(Asc(Data[1])) & "."
Print "    V:" & Hex(Asc(Data[2])) & "."
Print "    B:" & Hex(Asc(Data[3])) & "."
Another variant of your last choice:

Code: Select all

Print "    A:" & Hex(Asc(*Data, 1)) & "."
Print "    R:" & Hex(Asc(*Data, 2)) & "."
Print "    V:" & Hex(Asc(*Data, 3)) & "."
Print "    B:" & Hex(Asc(*Data, 4)) & "."
Post Reply