How to append a Hexadecimal value to a string?

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
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

How to append a Hexadecimal value to a string?

Post by myleow »

Is there a way to calculate the length of a string, get the Hexadecimal value of it and append it to the string?

The Hexadecimal value cannot be a string representation, meaning that it has to be 0x1a form. Not '0x1a'.

Try the code below

Code: Select all

<?php
$real_hex_1 = 0x1a;
$real_hex_2 = "\x1a";

$hex_string = "abcdefghijklmnopqrstuvwxyz";  //26 characters

$fake_hex = dechex(strlen($hex_string));

echo "<br/>Real Hex #1 ==> $real_hex_1";
echo "<br/>Real Hex #2 ==> $real_hex_2";
echo "<br/>FAKE Hex ==> $fake_hex";
?>
Is there a way to write a function to produce a Real Hex that i can append to my variable?

Example

Code: Select all

<?php
$hex_variable ='';
$hex_variable .=$real_hex_1;
$hex_variable .=$real_hex_2;
$hex_variable .=$fake_hex;
?>
The concatenation should be a real hex in the place of $fake_hex.

I am writing a WBXML Encoder and require to contruct a hexadecimal to send back to the Client.

If you think my thinking is wrong and that $fake_hex will work for the WBXML Decoder then this saves me alot of trouble because i don't have to worry about the 0x[a-z0-9]{1,2}.

If you think my thinking is wrong then try

Code: Select all

<?php
$real_hex ='';
$real_hex .= 0x05;
$real_hex .= 0x1a;
$real_hex .= 0x02;

$fake_hex='';
$fake_hex .= '0x05';
$fake_hex .= '0x1a';
$fake_hex .= '0x02';

$better_hex = '';
$better_hex .= "\x05";
$better_hex .= "\x1a";
$better_hex .= "\x02";

echo "Is $real_hex equal to $fake_hex?<br/>  What i am asking for is a function that can give me the ability to concatenate real_hex or even better if i can get something that can give me $better_hex for concatenation";
?>
I hope i made the question clear, and thank you advance for your help.

Regards
Mian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]chr[/php_man]()
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post by myleow »

Isn't chr() used for decoding? I need to encode.

If you can tell me how to use chr() please do so, because i have no clue how to solve my problem with chr().

I want hexadecimal results and ord() doesn't do it either. Please read my post carefully.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I did read your post.. chr returns the character result of a given number.. course.. you could just ask printf or pack to convert the number to hex.. or use dechex there are many functions that do this conversion..
myleow
Forum Contributor
Posts: 194
Joined: Mon Jun 21, 2004 7:05 pm
Location: California

Post by myleow »

I haven't tried printf but all the other functions doens't give me what i want.

in the case of dechex() it gives me back say 1a after i decoded the number 26. If i append that to my string which contain

0x02
0x00

NOTE its not "0x02" and "0x00", they are totally different.

I will get 201a which is a wrong output that i want. Appending 0x1a is not the same as appending dechex(26). It gives different result.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

passing the number's you want to convert to the character (which is exactly what your better_hex is getting), you can run through chr, which will convert the number to the character it represents..
Post Reply