1

I saw some file generation codes which uses the pack function.

echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);

What this is doing exactly?

s signed short (always 16 bit, machine byte order)

I dont know why i am not able to understand this in realtime. Am not asking a theoretical answer

When i execute the php , file got generated with some stange characters which is not identified by ms excel.

I just want to know the use of pack(), where to use and how to use?

hakre
  • 193,403
  • 52
  • 435
  • 836
zod
  • 12,092
  • 24
  • 70
  • 106

2 Answers2

3

pack is generally used to generate raw binary data. For example, I saw it used to generate zip files before native functions/classes were available in PHP for that.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113
1

When would you need to use mysql_connect? When connecting to a MySQL database. Why? To connect to a MySQL database.

When you'll realize that you just got this bad-ass lines of Hex numbers and you need to convert them to ASCII, or when you want to generate a .exe from user input (don't), or when you want to write a Little-Endian encoder, or a bunch of other stuff, you'll use pack.

If you just want to understand what pack does, there's already a question for it. But WHEN to use it? Well, when you want to pack data into a binary string.

Community
  • 1
  • 1
Zirak
  • 38,920
  • 13
  • 81
  • 92