C Short data type and PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chaquino
Forum Newbie
Posts: 1
Joined: Wed Jan 20, 2010 7:03 pm

C Short data type and PHP

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: C Short data type and PHP

Post 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
Last edited by requinix on Thu Jan 21, 2010 3:07 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: C Short data type and PHP

Post by Weirdan »

Post Reply