I am doing a socket connection and writing a data through a socket,I need to and null string at end of my data like,
myMethod=ABC||10D=12345||phNumber=zzz||lang=english and at last after english I need to append null string,please help me with this.
Simply append NULL word which is : "\0"
NSString *nullEndedString = #"myMethod=ABC||10D=12345||phNumber=zzz||lang=english\0";
\0 means NULL
You can add null at the beginning of the character by using
char bytes[] = "\0name\0surname";
NSString * mystring = [NSString mystring];
[mystring initWithBytes:bytes length:sizeof(bytes)];
NSData * data = [mystring dataUsingEncoding:NSUTF8StringEncoding];
You can use above code according to your functionality.
Related
If I have
NSString *myString = #"string for data1"
const char *utfMyString = [myString UTF8String];
NSLog(#"length of my myString is %d",[myString length]);
NSLog(#"length of my utfMyString is %lu",strlen(utfMyString));
I looked up the definition of method UTF8String which returns a null terminated at the end for the receiver. Therefore, i think length of utfMyString should be 17.
Unfortunately, utfMyString still is length of 16.
Please correct me about this issue. Thanks a lot
strlen() doesn't include the terminating null character in its length count.
This code SHOULD clean phone number, but it doesn't:
NSLog(#"%#", self.textView.text);
// Output +358 40 111 1111
NSString *s = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#", s);
// Output +358 40 111 1111
Any ideas what is wrong? Any other ways to remove whitespacish characters from text string (except the hard way)?
Try this
NSCharacterSet *dontWantChar = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *string = [[self.textView.text componentsSeparatedByCharactersInSet:dontWantChar] componentsJoinedByString:#""];
The documentation for stringByTrimmingCharactersInSet says:
Returns a new string made by removing from both ends of the
receiver characters contained in a given character set.
In other words, it only removes the offending characters from before and after the string any valid characters. Any "offending" characters are left in the middle of the string because the trim method doesn't touch that part.
Anyways, there are a few ways to do the thing you're trying to do (and #Narayana's answer is good on this, too... +1 to him/her). My solution would be to set your string s to be a mutable string and then do:
[s replaceOccurrencesOfString: #" " withString: #"" options: NSBackwardsSearch range: NSMakeRange( 0, [s length] )];
I have some data coming in from my DB (SQL Server 2008) and it has been formatted with char(10) as the line feeds and I want to replace them on my iOS device with \n.
How would I search for the char(10)? I know I would do replaceOccurancesOfStringWith but I need to nail this character down.
Make an NSString to replace using stringWithCString:encoding:, something like
// Make the string to find
char str[2] = { 10, 0 };
NSString *toFind = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
// Now do your replace :)
NSSTring *out = [input stringByReplacingOccurancesOfString:toFind withString:#"\\n"];
Say I have a string like "123alpha". I can use NSNumber to get the 123 out, but how can I determine the part of the string that NSNumber didn't use?
You can use NSScanner to both get the value and the rest of the string.
NSString *input = #"123alpha";
NSScanner *scanner = [NSScanner scannerWithString:input];
float number;
[scanner scanFloat:&number];
NSString *rest = [input substringFromIndex:[scanner scanLocation]];
If it is important to know exactly what is left after parsing the value this is a better approach than trying to trim characters. While I can't think of any particular bad input at the moment that would fail the solution suggested by the OP in the comment to this answer, it looks like a bug waiting to happen.
if your numbers are always at the beginning or end of a string and you want only the remaining characters, you could trim with a character set.
NSString *alpha = #"123alpha";
NSString *stripped = [alpha stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]];
If its starts out as a char * (as opposed to an NSString *), you can use strtol() to get the number and discover where the number ends in a single call.
when i use this code for generate an hash256 in my iPhone app:
unsigned char hashedChars[32];
NSString *inputString;
inputString = [NSString stringWithFormat:#"hello"];
CC_SHA256([inputString UTF8String],
[inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding ],
hashedChars);
NSData * hashedData = [NSData dataWithBytes:hashedChars length:32];
The hash256 of inputString, is created correctly, but if i use a string like this #"\x00\x25\x53\b4", the hash256 is different from the real string with "\x" characters.
I think that the problem is in encoding "UTF8" instead of ascii.
Thanks!
I would be suspicious of the first character, "\x00" - thats going to terminate anything that thinks its dealing with "regular C strings".
Not sure whether lengthOfBytesUsingEncoding: takes that stuff into account, but its something I'd experiment with.
You're getting the bytes with [inputString UTF8String] but the length with [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding]. This is obviously wrong. Moreover (assuming you mean "\xB4" and that it turns into something not in ASCII), "\xB4" is not likely to be in ASCII. The docs for NSString say
Returns 0 if the specified encoding cannot be used to convert the receiver
So you're calculating the hash of the empty string. Of course it's wrong.
You're less likely to have problems if you only generate the data once:
NSData * inputData = [inputString dataUsingEncoding:NSUTF8StringEncoding];
CC_SHA256(inputData.bytes, inputData.length, hashedChars);