|
|
Help Convert Obj-C Code to PHP
|
|
|
|
Fresh-Faced Recruit
Join Date: Oct 2007
Status:
Offline
|
|
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)
|
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Oct 2005
Location: Houston, TX
Status:
Offline
|
|
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Oct 2007
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Originally Posted by hally_
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;
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Oct 2007
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Posting Junkie
Join Date: Jun 2001
Location: Baltimore, MD
Status:
Offline
|
|
Depending on the datatype you're using the PHP equivalent of (size_t)length is probably sizeof().
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: May 2003
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Oct 2007
Status:
Offline
|
|
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);
}
?>
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Apr 2001
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|