Re: UInt8 > NSString
Re: UInt8 > NSString
- Subject: Re: UInt8 > NSString
- From: Dominik Pich <email@hidden>
- Date: Mon, 5 Mar 2007 14:47:48 +0100
Easiest would be to modify the method to return a NSString, I think.
Code:
--------
NSString mainMacAddress()
{
kern_return_t kernResult = KERN_SUCCESS; // on PowerPC this is an
int (4 bytes)
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
NSString string;
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult) {
//printf("FindEthernetInterfaces returned 0xx\n",
kernResult);
string =[NSString stringWithString:@"000000000000"];
} else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof
(MACAddress));
if (KERN_SUCCESS != kernResult) {
string =[NSString stringWithString:@"000000000000"];
} else {
string =[NSString stringWithFormat@"xxxxxx",
MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3],
MACAddress[4], MACAddress[5]];
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
return string;
}
----
EOF - Typed in mail so there can be syntax errors.
If you instead, prefer to keep the method and not modify it.
use string =[NSString stringWithFormat:@"%s" resultingCharPointer];
or string =[NSString stringWithBytes:resultingCharPointer
length:strlen(resultingCharPointer)+1];
or string =[NSString stringWithBytes:resultingCharPointer length:12 /
*hardcode same buffersize as in method*/];
Regards,
Dominik
Am Mar 5, 2007 um 2:19 PM schrieb arri:
hoi list,
i'm aware my question comes from the lack of knowledge about
datatypes and pointers (C, carbon etc..)
but for now i'd like to get this working quickly.
i'm using the 'GetPrimaryMACAddress' example from apple's site in
order to retrieve the main MAC-address of the local machine;
http://developer.apple.com/samplecode/GetPrimaryMACAddress/index.html
the example outputs to stdout, now i modified it so that the main()
function is a function i can call from cocoa like this:
static char * mainMacAddress()
{
kern_return_t kernResult = KERN_SUCCESS; // on PowerPC this is an
int (4 bytes)
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
char addr[12];
kernResult = FindEthernetInterfaces(&intfIterator);
if (KERN_SUCCESS != kernResult) {
//printf("FindEthernetInterfaces returned 0xx\n",
kernResult);
sprintf(addr,"000000000000");
} else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof
(MACAddress));
if (KERN_SUCCESS != kernResult) {
sprintf(addr,"000000000000");
} else {
sprintf(addr,"xxxxxx",
MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3],
MACAddress[4], MACAddress[5]);
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
return (char*) addr;
}
and then elsewhere i call that function and create an NSString with
the result like this:
char *adr = mainMacAddress();
macAddress = [NSString stringWithCString:adr];
the compiler gives me a warning (function returns address of local
variable), but it works on intel-mac. on ppc it just returns rubbish.
what would be the correct way of doing what i want?
thanks,
arri
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden