Hey I have stuck on how to convert my int value to NSString and get correct value
I have unsigned int
unsigned int r = 0xEFF;
and I like to have NSString like #"EFF", not #"3839"
You can use the same format strings as printf:
NSString* newString = [NSString stringWithFormat:#"%x", r];
Related
I have an NSData object whose contents are raw bytes looking something like this:
1e050014 c8d7b452 28f98c72 e95748b9 2801086b e85b07b9 2c010054 01000014
88c9b452 68878a72 e95748b9 2801086b e85707b9 20030154 10050014 a84bb552
c8299a72 e95748b9 2801086b e85307b9 2c010054
I'm trying to put these in a string array as is and this hasn't worked and returns an empty array:
NSData* data0 = [NSData dataWithContentsOfFile:str0];//this contains the bytes well
NSMutableArray *array = [NSMutableArray arrayWithCapacity:data0.length];
This doesn't work either:
const char* fileBytes = (const char*)[data0 bytes];
for (int i = 0; i < data0.length; i++) {
UInt8 byteFromArray = fileBytes[i];
[array addObject:#(byteFromArray)];
}
How can I put the raw bytes into a string array without interpreting the raw bytes as strings?
I'm trying to encrypt string in Xcode to PHP with AES128 method by using following code:
Xcode
- (NSData*)AES256EncryptWithKey:(NSString*)key {
char keyPtr[kCCKeySizeAES256];
[key cStringUsingEncoding:NSASCIIStringEncoding];
NSString *iv = #"fdsfds85435nfdfs";
char ivPtr[kCCKeySizeAES128];
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSASCIIStringEncoding];
NSUInteger dataLength = [self length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void* buffer = malloc(bufferSize);
size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, NULL,
keyPtr, kCCKeySizeAES256,
ivPtr,
[self bytes], dataLength,
buffer, bufferSize,
&numBytesEncrypted);
if (cryptStatus == kCCSuccess)
{
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
}
free(buffer); //free the buffer;
return nil;
}
But when I run above coding, following result is not what I expect to be:
NSString *key = #"89432hjfsd891787";
NSData *plaintext = [[#"aaa0000000000000" dataUsingEncoding:NSASCIIStringEncoding] AES256EncryptWithKey: key];
NSString *mystring = [[NSString alloc] initWithData:plaintext encoding:NSASCIIStringEncoding];
NSLog(#"mystring %#", mystring);
OUTPUT is
uçó)â½S/èRÅ
What I want it something like that.
m9FNGM9IiwibWFjIjoiNmJkYzNmZTA5
Note: due to a coding error the keyPtr is not set to the key value, it becomes the value of the uninitialized memory.
You get "uçó)â½S/èRÅ" because you try to create a string from the data and there are a couple reasons this will not work.
Many data bytes do not map to printable ASCII characters or to any unicode character.
A data byte can be 0 (1 in 256 bytes on average will be 0x00) and that is a terminator for a "C" string so the string will be short.
There are two general conventions for encoding data into a string representation:
Hex-ascii where each data byte is encoded into two characters 0-9a-f.
Base64 where each 3 data data bytes are encoded into 4 ASCII characters.
You are probably looking for Base64 encoding:
NSString *mystring = [plaintext base64EncodedStringWithOptions:0];
Other errors in the code:
[key cStringUsingEncoding:NSASCIIStringEncoding]; The string may not be ASCII, better to use NSUTF8StringEncoding. Note that the output is not captured.
The keyPtr is never set to the key value, see above.
The key size id specified as 256-bits (32-bytes) but the key is 16 characters, use a key size that matched the key.
char ivPtr[kCCKeySizeAES128], an iv size is the block size not the key size: kCCBlockSizeAES128.
Looks like you want a Base64 encoded string.
NSString *mystring = [plaintext base64EncodedStringWithOptions:0];
I am working on a Cocoa Mac OSX app, and I am wondering if it is possible to present the contents of an NSRange found by:
NSRange range;
range.location = 4;
range.length = 4;
as an NSString?
e.g. in the example above, if I had a string with contents "abcdefgh", presenting the contents of the above range as a string would give "efgh". Is this possible?
Code:
NSString *string = #"abcdefgh";
NSRange range;
range.location = 4;
range.length = 4;
NSString *subString = [string substringWithRange:range];
NSLog(#"%#",subString);
Output:
efgh
Try the method substringWithRange from NSString.
NSString* original = #"abcdefgh";
NSLog(#"Substring: %#", [original substringWithRange:range]);
I found out in Xcode command line tool you can enter int into the code yourself with scanf.
When I tried this for a NSString, it didn't worked, and I found out scanf returns an integer, so my question is, what do you use to enter a NSString and save it into a variable, like:
int number;
scanf("%i", &number);
EDIT:
Now I found a code, but it only shows the first char:
char naamchar[40];
int nChars = scanf("%39s", naamchar);
NSString * naam = [[NSString alloc] initWithBytes:naamchar
length:nChars
encoding:NSUTF8StringEncoding];
naam is only 1 char :(
EDIT SOLUTION:
char naamchar[40];
scanf("%39s", naamchar);
NSString * naam = [NSString stringWithCString:naamchar encoding:NSUTF8StringEncoding];
...
man 3 scanf mentions:
These functions return the number of input items assigned.
nChars is set returning the number of items matched, in this case 1, not the number of characters in the string.
try replacing nChars with strlen(naamchar) i.e.
char naamchar[40];
int nChars = scanf("%39s", naamchar);
NSString * naam = [[NSString alloc] initWithBytes:naamchar
length:strlen(naamchar)
encoding:NSUTF8StringEncoding];
be sure to check for nChars == 0, which would indicate that there was no input to scan.
I have a C function with the following method signature.
NSString* md5( NSString *str )
How do I call this function, pass in a string, and save the returned string?
I tried the following, but it did not work:
NSString *temp= [[NSString alloc]initWithString:md5(password)];
thanks for your help
You're making it too hard. The stuff in []'s is effectively smalltalk. What you want is to just call the function in C:
NSString * temp = md5(password);
What is password? Is password a common "char *" pointer? Is the md5 signature you put correct?
If that's the case, you could:
NSString *temp = [[NSString alloc] initWithCString:password encoding:NSASCIIStringEncoding];
If your md5 signature is: char *md5(char *password), and you have you password stored in a NSString, you could:
NSString password = #"mypass";
char buff[128];
NSString *temp = [[NSString alloc] initWithString:password];
[temp getCString:buff maxLength:128 encoding:NSASCIIStringEncoding];
char *md5 = md5(buff);
// then you could do whatever you want with md5 var