user220201
user220201

Reputation: 4532

Storing a hex string in php

Php newbie here. Trying to have a fixed key in my code. How do I do this?

In C for example I do this --

unsigned char key[] = { 0x3f, 0xee, 0x40, 0x93, 0x2f, 0xca, 0x7b, 0xdf, 0x9f, 0x04, 0x23, 0x62, 0x9e, 0x33, 0x98, 0x4f };

How do I do this php? I know I have not completely understood the string concept in php but there is no concept of a byte array in php but how does php handle binary data? I mean specifically signed and unsigned characters.

I am guessing I could use the hex2bin somehow. Is this a correct representation of the above key in the C array?

$key = hex2bin('3fee40932fca7bdf9f0423629e33984f');

EDIT: What is the standard practice to represent a fixed key in code?

Upvotes: 3

Views: 4479

Answers (1)

mingos
mingos

Reputation: 24502

The PHP equivalent of your C code would be:

$key = array(
  0x3f,
  0xee,
  0x40,
  /* etc. */
);

PHP does things in a smart way: it handles your data types for you. There is no such thing as an "unsigned char". There's just an integer, which can easily become a string or a float if you use the right operators on it. The concept of unsigned numbers is foreign to PHP. They are all signed. If you wish your 0x80 to become -128, you'll need to look for a more PHP-ish solution ;).

Upvotes: 2

Related Questions