Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PACK() in PHP via VFP 2

Status
Not open for further replies.

vie3548

Programmer
Jul 12, 2021
28
AE
hi,

can anyone help pls..
i have php code which i need to be converted in vfp.
i need to get same result.
have anyone tried this.. pls help
i have sprintf and base64_encode routine.

for sprintf(), i can get same result using :
RIGHTC(Transform(LEN(CREATEBINARY(1)), '@02X'),2)

PHP

$tag = pack('H*', sprintf("%02X",1));
$value = "My Shop";
$length = pack('H*', sprintf("%02X",strlen($value)));
$lenVal = sprintf("%02X",strlen($value)) ;
echo base64_encode($tag.$length.$value) ;

Result :

AQdNeSBTaG9w


the problem is Pack(), how to translate in vfp
Thanks for help

Vie
 
pack() with the format code 'H*' converts to a hexadecimal string. So the VFP equivalent would be [tt]TRANSFORM(x, "@0"[/tt]), where x is the numeric value that you want to convert. Note that "@0" is a zero, not the letter O.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
hi sir Mike,

i tried this, but not getting same result.

cValue="My Shop"
cTag=RIGHTC(Transform(1, '@02X'),2)
cLen=RIGHTC(Transform(LEN(CREATEBINARY(cValue)), '@02X'),2)
?base64_encode(cTag+cLen+cValue)

FUNCTION base64_encode(S as String)
LOCAL i,j,k,q,ch,s2,buf,basechars
basechars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
buf = 0
s2 = ''
j = 0
k = LEN(s)
FOR i = 1 TO k
ch = ASC(SUBSTR(s,i,1))
q = (BITAND(BITRSHIFT(ch,6),3)) + BITLSHIFT(BITAND(BITRSHIFT(ch,4),3),2) + ;
BITLSHIFT(BITAND(BITRSHIFT(ch,2),3),4) + BITLSHIFT(BITAND(ch,3),6)
buf = BITOR(BITLSHIFT(q,j),buf)
j = j + 8
DO WHILE (j >= 6) OR ((i = k) AND (j > 0))
q = 1 + BITLSHIFT(BITAND(buf,3),4) + BITLSHIFT(BITAND(BITRSHIFT(buf,2),3),2) + ;
(BITAND(BITRSHIFT(buf,4),3))
ch = SUBSTR(basechars,q,1)
s2 = s2 + ch
buf = BITRSHIFT(buf,6)
j = j - 6
ENDDO
ENDFOR
s2 = s2 + IIF(LEN(s2) % 4 > 0,REPLICATE('=',4 - (LEN(s2) % 4)),'')
RETURN s2
ENDFUNC


Thanks for help.

Vie
 
I don't quite understand how this should work, but VFP has a simple way to convert to base64 and back with STRCONV.

Code:
? STRCONV("AQdNeSBTaG9w",14)
mainly returns the word MyShop, prefixed with an unprintable character which VFP displays with an empty square.
That's not what you encode in base64 in your example.

The first character of the result is CHR(1)

If AQdNeSBTaG9w would be the right value then $tag would be empty, $length would be 1 (Chr(1)) and $value would be 'MyShop'. That does not make sense to me.



Chriss
 
Elvie, sorry if I misled you. I'm not quite clear what data type you are trying to convert to. Are you looking for a numeric value expressed in hex? The TRANSORM() function always returns a character string.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
hi Sir Chris,

I agree with you.I, too was confused on how their tool works.
how to explain this.
am generating barcode thru our software, this barcode has to be validated in third party tool.
third party tool works that way (php code).
to be able to get a barcode which is compliant to the third party tool.. i have to translate
the PHP coding in vfp to get same result.

how the tool works:
string is encoded to TLV (type-length-value or tag-length-value) data structure.
ex : My Shop

type = 1 as a type (which is fixed)
length = number of the bytes of string
value = My Shop
then, the tlv list is converted to hex then to base64


PHP

$tag = pack('H*', sprintf("%02X",1));
$value = "My Shop";
$length = pack('H*', sprintf("%02X",strlen($value)));
$lenVal = sprintf("%02X",strlen($value)) ;
echo base64_encode($tag.$length.$value) ;

Result :

AQdNeSBTaG9w


Vie

 
Vie,

This is the VFP equivalent of the PHP code you posted:

Code:
LOCAL FieldTag AS Integer
LOCAL TextValue AS String
LOCAL LenValue AS Integer

m.FieldTag = 1
m.TextValue = "My Shop"
m.LenValue = LEN(m.TextValue)

? STRCONV(BINTOC(m.FieldTag, "1RS") + BINTOC(m.LenValue, "1RS") + m.TextValue, 13)
 
hi sir Atlopes,

It worked. Thank you very very much for help. [smile2]


Vie
 
hi Sir Mike,

it's ok. me too was confused.
but it worked now.
Thank you for giving time. [smile]


Vie
 
Vie,

Good to see that Sir Atlopes has solved the problem for you - much better than I could have done.

Don't forget that you can flag the solution with a red star, which indicates that you found it particularly helpful. This also helps other people looking for a solution to a similar problem. To do so, just click the "Great post" icon in the bottom right of Atlope's post.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
hi Sir Atlopes,

just got one more question.

how to use BINTOC(1, "1RS") in vfp6.
RS is only available in 9.
Thank you.


Vie
 
hi Sir,

i got it solved.
Thank you for your time [smile]


Vie
 
Well,

I am a bit puzzled because inversing AQdNeSBTaG9w I don't get the necessary chr(1)+chr(5)+"MyShop", but I see I failed to analyze the second byte.

It's a bit simpler than atlopes expression, if you just convert the type and length with chr:
Code:
? STRCONV(CHR(m.FieldTag) + CHR(m.LenValue) + m.TextValue, 13)

More important than this being shorter, atlopes use of BINTOC will convert a signed bytes, values from -128to 127, whereas I think the type and length can only be positive. I don't think you have more than 127 types, but with BINTOC(m.LenValue, "1RS") you could only correctly have lengths from 1 to 127. This may also never be a problem, but to encode lengths 128 to 255 you better use CHR(m.LenValue).

And just by the way, even in PHP you have chr, you don't need pack to convert a value from 0 to 255 to na ascii char. CHR() does that very well and much simpler.


Chriss
 
Hi Sir Chris,

It worked. getting same result and much simpler.
Thank you for your time, it was really helpful. [smile2]

Regards,
Vie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top