473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

bit handling function

hello,

can anyone help me out with a bit handler?

it seems as if php's decbin() removes leading zeroes when converting
from hex....so for example decbin(hexdec()); basically removes any
leading zeroes.

i'm trying to convert some 32bit hex words into bin, and the first 12
bits need to be broken down into 3-bit sets for decoding a key of
sorts. for example:

(on paper)
0x2101769:
-------normal bin------- ----3-bit sets----
0010 0001 0000 0001 011 101 101 001

(in php)
decbin(hexdec(2101769)) = 1000000001001000001001
are there any functions that could break apart the number once i get
the conversion done correctly?
thanks!
Jul 11 '08 #1
6 1743
..oO(inexion)
>can anyone help me out with a bit handler?

it seems as if php's decbin() removes leading zeroes when converting
from hex....so for example decbin(hexdec()); basically removes any
leading zeroes.
Correct, because in a number leading zeros don't have any meaning:
1 = 01 = 001 ...
>i'm trying to convert some 32bit hex words into bin, and the first 12
bits need to be broken down into 3-bit sets for decoding a key of
sorts. for example:

(on paper)
0x2101769:
-------normal bin------- ----3-bit sets----
0010 0001 0000 0001 011 101 101 001

(in php)
decbin(hexdec(2101769)) = 1000000001001000001001
are there any functions that could break apart the number once i get
the conversion done correctly?
thanks!
Pad the string with zeros to the required length of at least 12 chars,
take the last 12 chars from it and split them into groups of three:

<?php
$hexCode = '2101769';
$binCode = substr(sprintf('%012b', hexdec($hexCode)), -12);
$parts = str_split($binCode, 3);
var_dump($parts);
?>

HTH
Micha
Jul 11 '08 #2
In article <80**********************************@e39g2000hsf. googlegroups.com>,
inexion <ab******@gmail.comwrote:
>it seems as if php's decbin() removes leading zeroes when converting
from hex....so for example decbin(hexdec()); basically removes any
leading zeroes.
If you want leading zeros in a 32-bit binary representation, try
$binary = sprintf('%032b', $number);
>i'm trying to convert some 32bit hex words into bin, and the first
12 bits need to be broken down into 3-bit sets for decoding a key
Lowest 3 bits:
$low3 = sprintf('%03b', $number & 0x0003);

Next 3 bits:
$next3 = sprintf('%03b', ($number >3) & 0x0003);

Next 3 bits:
$another3 = sprintf('$03b', ($number >6) & 0x0003);

....and so on.

-A
Jul 12 '08 #3
Thanks for the replies guys -
i got it working with this code:

$tnum = array();
$tnum[] = hexdec($targets['flags'][$i]);
$binCode = substr(sprintf('%012b', $tnum[$i]), -12);
$parts = str_split($binCode, 3);
echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
$parts[3]."<br />";

thanks!!
Jul 14 '08 #4
Thanks for the replies guys -
i got it working with this code:

$tnum = array();
$tnum[] = hexdec($targets['flags'][$i]);
$binCode = substr(sprintf('%012b', $tnum[$i]), -12);
$parts = str_split($binCode, 3);
echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
$parts[3]."<br />";

thanks!!
Jul 14 '08 #5
Thanks for the replies guys -
i got it working with this code:

$tnum = array();
$tnum[] = hexdec($targets['flags'][$i]);
$binCode = substr(sprintf('%012b', $tnum[$i]), -12);
$parts = str_split($binCode, 3);
echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
$parts[3]."<br />";

thanks!!
Jul 14 '08 #6
..oO(inexion)
>Thanks for the replies guys -
i got it working with this code:

$tnum = array();
$tnum[] = hexdec($targets['flags'][$i]);
$binCode = substr(sprintf('%012b', $tnum[$i]), -12);
$parts = str_split($binCode, 3);
echo "flags: ".$parts[0]." ".$parts[1]." ".$parts[2]." ".
$parts[3]."<br />";
I would write the last line as

echo "flags: {$parts[0]} {$parts[1]} {$parts[2]} {$parts[3]}<br />";

But that's just personal preference.
>thanks!!
You're welcome.

Micha
Jul 15 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
2
by: Paul E. Orman | last post by:
I have a piece of VB code (.NET 1.1 - VB 2003) that loads data from a database through a timer. So the timer is setup and from it I call the procedure that loads the latest records from the...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
33
by: Anthony England | last post by:
I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on...
10
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have...
5
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored...
1
by: pob | last post by:
>From a form I have some code that calls 4 modules frmMain 1 mod 2 mod 3 mod 4 mod If mod 1 experiences an error the error handling works fine within mod 1 and writes out the error to a...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
0
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access...
1
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.