Page 1 of 1

C Short data type and PHP

Posted: Thu Jan 21, 2010 12:24 am
by chaquino
Hello all,

I have some PHP scripts that need to communicate to a server running some apps that returns mostly text, except for one parameter at the very beginning - a record length, which is a C-Short data type.

If everything else is text, then I just need to figure out how to parse this C-short data type variable's value before being able to find out where the rest of the text is but need some advice as how to go about doing this.

One simple workaround is to have the apps be modified so that they return this length in plain text but that's not possible.

Any help would be greatly appreciated!

Thanks

Re: C Short data type and PHP

Posted: Thu Jan 21, 2010 1:46 am
by requinix
So you're getting two bytes representing a C-style short int?

Use ord to get the numerical value of a byte. Then it's just some base-256 conversion.

Code: Select all

Long int \x12\x34\x56\x78 => characters 18, 52, 86, 120
 
Big endian:    18 * 256^3 + 52 * 256^2 + 86 * 256^1 + 120 * 256^0 = 305419896
Little endian: 18 * 256^0 + 52 * 256^1 + 86 * 256^2 + 120 * 256^3 = 2018915346
Ah, right, unpack... I thought I was forgetting something

Re: C Short data type and PHP

Posted: Thu Jan 21, 2010 3:00 am
by Weirdan