Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Help Convert Obj-C Code to PHP

Help Convert Obj-C Code to PHP
Thread Tools
hally_
Fresh-Faced Recruit
Join Date: Oct 2007
Status: Offline
Reply With Quote
Oct 23, 2007, 10:05 PM
 
Hi All,

I'm delving into Obj-C and php and I am wondering if someone can help me translate the code below to php. I am particularly interested in what the & does and what the >> does, what 'A' is and what 0x00f0 means; the rest of it I can understand. Thanks in advance for any help, I really appreciate it.

NSString *myStringToChange = @"hello world";
NSMutableString *newString = [[[NSMutableString alloc] init autorelease];
//conver to an NSData object
NSData *data = [myStringToChange dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

unsigned char* buffer = (unsigned char*) [data bytes];

size_t length = [data length];
while (length>0) {

char c[3] = {0,0};
c[0] = ((buffer[length-1] & 0x00f0) >> 4);

if (c[0] > 9) {
c[0] += 'A' - 10;
} else {
c[0] += '0';
}

[newString insertString:[NSString stringWithCString:c] atIndex:0];
length--;
}
( Last edited by hally_; Oct 23, 2007 at 10:06 PM. Reason: typo in code)
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Oct 23, 2007, 11:16 PM
 
& is a bitwise and.
0x00f0 is a hexadecimal integer literal and is the same as 240 in decimal.
     
mduell
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status: Offline
Reply With Quote
Oct 23, 2007, 11:57 PM
 
>> is a bit shift
     
hally_  (op)
Fresh-Faced Recruit
Join Date: Oct 2007
Status: Offline
Reply With Quote
Oct 24, 2007, 07:57 AM
 
Thanks for the responses so far.

So in the code I pasted, it is saying:

Loop through the bits in the buffer array, starting at the end and going backwords. Then but each bit into an array, called c, but when you do so, set the bits that go into c as the bits in the big array at the specified index + the bits in 0x00f0, shifted over 4 places?

Can someone tell me what the 'A' is?

If if someone really wants to make my day, transcribe it into php
     
nonhuman
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Oct 24, 2007, 11:13 AM
 
Originally Posted by hally_ View Post
Thanks for the responses so far.

So in the code I pasted, it is saying:

Loop through the bits in the buffer array, starting at the end and going backwords. Then but each bit into an array, called c, but when you do so, set the bits that go into c as the bits in the big array at the specified index + the bits in 0x00f0, shifted over 4 places?

Can someone tell me what the 'A' is?

If if someone really wants to make my day, transcribe it into php
A is the ASCII character 'A'.

The ASCII value of 'A' is 65, so that line is saying: c[0] += 55;
     
hally_  (op)
Fresh-Faced Recruit
Join Date: Oct 2007
Status: Offline
Reply With Quote
Oct 24, 2007, 09:14 PM
 
Ok, I think I'm making some progress. My biggest stumbling block now is finding the php functions that match the C/Obj-C functions. So far I have these, please let me know if these functions are equivalent or if you know what the correct function is.

On the left is the Obj-C function, on the right after the "= >" is the proposed php function.

-(NSData*)dataUsingEncoding: allowLossyConversion: = > utf8_encode()
-(const void*)bytes = > unpack() ????
-(size_t)length = > ????

Thanks again everyone, I appreciate it.
     
nonhuman
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status: Offline
Reply With Quote
Oct 25, 2007, 10:02 AM
 
Depending on the datatype you're using the PHP equivalent of (size_t)length is probably sizeof().
     
fugu13
Fresh-Faced Recruit
Join Date: May 2003
Status: Offline
Reply With Quote
Oct 25, 2007, 10:05 AM
 
Yes, utf8_encode does a similar thing, but depending on the context, you might not need to use it.

The other two things are not functions/methods they are a cast and a type statement (well, in the original code. You've rewritten them both like casts).

You probably can just ignore those bits. For instance, the line "size_t length = [data length];"

length is just a variable, and it is being assigned the length of the data variable. Equivalent PHP would be something like:

$length = count($data);

The basic issue is, the Objective-C code requires a lot more low-level/type-related manipulation than 'equivalent' PHP code would. Of course, the PHP code will probably be a couple orders of magnitude slower.
     
hally_  (op)
Fresh-Faced Recruit
Join Date: Oct 2007
Status: Offline
Reply With Quote
Oct 25, 2007, 09:15 PM
 
Well I'm not having a very good time with php, there are sooo many functions (but I guess that can be a good thing ;-)). Anyway, this is what I have so far:

<?php

$data = utf8_encode("hello world");
$length = count($data);

while ($length>0) {
//this line is definitely incorrect, how do I access the items in the $data array?
$c[] = ((substr($data,$length -1, 1) & 0x00f0) >> 4);

$length--;
echo($c);
}

?>
     
wadesworld
Grizzled Veteran
Join Date: Apr 2001
Status: Offline
Reply With Quote
Nov 4, 2007, 01:18 PM
 
If you told people what the goal is, it'd be easier to help.

Translating from Obj-C to PHP line for line is not always viable - they're used for vastly different things. But if we know the goal, we likely can tell you how to do it in PHP.
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 04:26 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,