×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PACK() in PHP via VFP
2

PACK() in PHP via VFP

PACK() in PHP via VFP

(OP)
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

RE: PACK() in PHP via VFP

pack() with the format code 'H*' converts to a hexadecimal string. So the VFP equivalent would be TRANSFORM(x, "@0"), 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

RE: PACK() in PHP via VFP

(OP)
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

RE: PACK() in PHP via VFP

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

RE: PACK() in PHP via VFP

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

RE: PACK() in PHP via VFP

(OP)
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

RE: PACK() in PHP via VFP

Vie,

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

CODE --> VFP

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) 

RE: PACK() in PHP via VFP

(OP)
hi sir Atlopes,

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


Vie

RE: PACK() in PHP via VFP

(OP)
hi Sir Mike,

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


Vie

RE: PACK() in PHP via VFP

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

RE: PACK() in PHP via VFP

(OP)
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

RE: PACK() in PHP via VFP

Quote (Vie)

how to use BINTOC(1, "1RS") in vfp6.

In this case, CHR() will do.

RE: PACK() in PHP via VFP

(OP)
hi Sir,

i got it solved.
Thank you for your time smile


Vie

RE: PACK() in PHP via VFP

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

RE: PACK() in PHP via VFP

(OP)
Hi Sir Chris,

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

Regards,
Vie

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close