PHP Programing language

bin2hex()and pack() Functions in PHP
Previous Home Next

bin2hex()

The bin2hex() function converts a string of ASCII characters to hexadecimal values. The string again converted back by using the pack() function.

Syntax:
bin2hex(string)

Example:

<?php 
$str = bin2hex("Gud Morning India!");
echo($str); 
?>

 

Output:

477564204d6f726e696e6720496e64696121

pack()

The pack() function packs data into a binary string.

Syntax:
pack(format,args+)

In above syntax, "args+" specifies one or more arguments to be packed and format is as fallows:

  • a - NUL-padded string
  • A - SPACE-padded string
  • h - Hex string, low nibble first
  • H - Hex string, high nibble first
  • c - signed char
  • C - unsigned char
  • s - signed short (always 16 bit, machine byte order)
  • S - unsigned short (always 16 bit, machine byte order)
  • n - unsigned short (always 16 bit, big endian byte order)
  • v - unsigned short (always 16 bit, little endian byte order)
  • i - signed integer (machine dependent size and byte order)
  • I - unsigned integer (machine dependent size and byte order)
  • l - signed long (always 32 bit, machine byte order)
  • L - unsigned long (always 32 bit, machine byte order)
  • N - unsigned long (always 32 bit, big endian byte order)
  • V - unsigned long (always 32 bit, little endian byte order)
  • f - float (machine dependent size and representation)
  • d - double (machine dependent size and representation)
  • x - NUL byte
  • X - Back up one byte
  • Z - NUL-padded string
  • @ - NUL-fill to absolute position

Example:

<?php
$str = "Gud Morning!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>  

Output:

477564204d6f726e696e6721
Gud Morning!

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: