472,783 Members | 950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Convert 4-byte string to integer

Ive used fwrite to store 4 bytes in a file: "Test" (or 0x54657374 /
1953719636). Its easy enough to use fread to get those 4 bytes, but the
problem is I need to work with the data as a number and not a string, so how
can I convert it to the number 1953719636 ?
Jul 17 '05 #1
7 10634
"Dave Turner" <no****@nowhere.nohow> wrote in message
news:42********@quokka.wn.com.au...
Ive used fwrite to store 4 bytes in a file: "Test" (or 0x54657374 /
1953719636). Its easy enough to use fread to get those 4 bytes, but the
problem is I need to work with the data as a number and not a string, so
how can I convert it to the number 1953719636 ?


http://php.net/unpack
Jul 17 '05 #2
Example please? It doesnt seem to be working ...

<?
$packed = "Test";
$unpacked = unpack("L", $packed);
echo $unpacked;
?>

It should echo 1953719636, but it's not displaying anything

Jul 17 '05 #3
Hmmm, ok :-) Well this works, but it doesnt seem very efficient:

<?
$Buffer = "Test";

$Number = ord($Buffer{0}) | (ord($Buffer{1})<<8) | (ord($Buffer{2})<<16) |
(ord($Buffer{3})<<24);

echo $Number;
?>

If anyone knows of a better method I'd appreciate it, thanks
Jul 17 '05 #4
"Dave Turner" <no****@nowhere.nohow> wrote:
Example please? It doesnt seem to be working ...

<?
$packed = "Test";
$unpacked = unpack("L", $packed);
echo $unpacked;
?>

It should echo 1953719636, but it's not displaying anything


It does work, but you didn't read the docpage well. unpack() returns an
array so use print_r() instead of echo.

E.
Jul 17 '05 #5
> It does work, but you didn't read the docpage well. unpack() returns an
array so use print_r() instead of echo.

Thankyou for clarifying that, "echo $unpacked[1];" also works

So i guess my final question is - what is more efficient? unpack() or 4 x
Ord() ?
Jul 17 '05 #6
"Dave Turner" <no****@nowhere.nohow> wrote:
So i guess my final question is - what is more efficient? unpack() or 4 x
Ord() ?


function utime()
{
list( $usec, $sec ) = explode( ' ', microtime() );
return ((float)$usec + (float)$sec);
}

$t = utime();
for ( $i = 0; $i < 1000000; ++$i )
{
//insert code for testing here
}
$t = utime() - $t;
echo "Time : $t sec\n";
Jul 17 '05 #7
Very useful timing routine, thanks. The unpack/array[1] method is faster by
about 25% over the 4 x ord() method
Jul 17 '05 #8

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

Similar topics

5
by: Cally | last post by:
Hello, I would like to convert a field from ntext field found in one database table to float field found in another database table. The reason why I want to do this is a long one. I have...
25
by: cory | last post by:
Hi, I have an Access database and am having an ASP.NEt application written for it. It is almost complete. I have a hosting company that I signed up with a month ago but before I did anything I...
5
by: Omey Samaroo | last post by:
Dear Access Gurus, I am tasked with converting an Access 97 Database to A2K. I created Macros, switchboards and used the full feature of buttons on forms. The A97 is also split (FE and BE). Are...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
2
by: Alberto | last post by:
why there isn't a Convert.ToFloat() method in the Convert class? Sometimes I can't work with a float field because there isn't this method and I need it to, for example, convert the input in a...
3
by: Thubaiti | last post by:
Hi, I have this code in my ASP.NET and I want to convert it to C# (code behind) <asp:Repeater id="subCategoryRepeater" runat="server"> <ItemTemplate> <ul> <li> <asp:HyperLink...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
5
by: melickas | last post by:
We designed a custom application using Office Developer Tools '97 which included a Run-time version of Access--- so it would not matter if our customer even had any version of Access on their...
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.