Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40503

Converting hexadecimal to byte in php

I am very new to PHP

I want to create a byte array something like this (in Java):

byte array[] = { (byte) 0x9C, (byte) 0xA0};

How do I do it in PHP? Any syntactical help highly apreciated.

Upvotes: 0

Views: 4325

Answers (2)

Pekka
Pekka

Reputation: 449415

What exactly do you mean by "hexadecimal to byte"? I am going to assume you mean "... to decimal".

The result of 0x3f when output will be automatically converted to a decimal number. In internal calculations, it will be converted automatically if needed - you can do

$myvar = 300 + 0xfa;

without problems.

You can cast a variable to an integer using (int)$varname or (int)value but it doesn't really make sense in your case. A byte is a byte, whether you express its value as 0x3F or 63.

To convert hexadecimal to decimal, there is also hexdec()

Returns the decimal equivalent of the hexadecimal number represented by the hex_string argument. hexdec() converts a hexadecimal string to a decimal number.

Upvotes: 1

Aif
Aif

Reputation: 11220

How about pack function?

Upvotes: 1

Related Questions