I lost % in html source when using stringWithContentsOfUrl - iphone

NSString *htmlSource = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:0x80000000 + kCFStringEncodingDOSKorean error:nil];
NSLog(htmlSource);
I get html result tag like (col width="16"/)~~~ but the real html tag is (col width="16%"/)
% character disappears. what is problem?

The string might still contain a %; NSLog() itself gives special significance to a % sign (consider what happens if you use %#, %d, etc.).
Try doing this: NSLog(#"%#", htmlSource); that will log only an object (the string) and keep it out of the formatting argument.

Related

importing tyhe csv file in ios sdk

In my app, I am trying to import the csv files as follow:
NSError *error;
NSString *path1=[[NSString alloc]initWithContentsOfFile:CSVPath encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#",path1);
NSArray *messArr=[path1 componentsSeparatedByString:#"\n"];
NSLog(#"%#",messArr);
Question:
When i try to log the array, it gives the last column values with many spaces like as follow:
Path1: student_name,gender,email_id
test1,male,a
test2,male,b
test3,male3,c
messArr:
(
"student_name,gender,email_id
",
"test1,male,a
",
"test2,male,b
",
"test3,male3,c"
)
Here i got the count is 4 but can't able to remove spaces.
So, I can't able to remove spaces from the messArr.
Why this happen? I don't know.
Help me to solve this problem.
i think there is a problem with which encoding scheme you used when you creating the csv file.
Thank you,
Please use CSV Parser available on Github repository.
Use CSV parser for Objective-C
Thanks,
you can remove the spaces
by using this meted on each object of messier
NSString *str=[[messArr objectAtIndex:index] stringByTrimmingCharactersInSet: whitespaceAndNewlineCharacterSet];
use this str for your purpose.
I think it is because of \n. if you dont want it then remove this line
NSArray *messArr=[path1 componentsSeparatedByString:#"\n"];
for showing line iterate your array in for loop, & hardcode the \n in the code
for(int i=0; i<[messArr count];i++)
{
NSLog(#"%#\n",[messArr objectAtindex:i]);
}

stringWithFormat and % not working

i have this string
NSString *jsonString = #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D";
NSLog(#"%#",jsonString);
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A721%7D
when i use
NSString *linkId = #"448";//not a constant value only for example
NSString *jsonString = [NSString stringWithFormat:#"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A%#%7D",linkId];
the output is
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=7 37040ate23A222㿠 37040isplay23A0x1.21800000507cp-1027ll27D&action=showMatches&params=7 –ompetition_id23A(null) 0
as you see not the same.My question is how to use stringWithFormat to get this result:
http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A448%7D
so the value (721) just at the and is replaced by (448)
thanks in advance.
It's because all those % characters inside your format string are being potentially used to used the format arguments, much like %# (see here for details).
This can be seen (for one instance) where:
callback_params=%7B%22date
is transformed into:
callback_params=7 37040ate
In that case, I'm not sure what the %7B is doing since it's not a valid format specifier, but the %22date is resulting in a 22-character decimal value, from %22d, followed by the literal ate.
You need to use %% in your format string if you want a single % in the output string.
The other way of looking at it is that the thing you're giving it as a format string is really data, not purely a format.
To be safe from those spurious conversions, you'd want:
NSString *jsonString = [NSString stringWithFormat:#"%#%#%#", #"http://www.soccerway.com/a/block_home_matches?block_id=block_home_matches_14&callback_params=%7B%22date%22%3A%222012-07-31%22%2C%22display%22%3A%22all%22%7D&action=showMatches&params=%7B%22competition_id%22%3A",linkId, #"%7D"];

Splitting a number off prefix of a string on iPhone

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.

Problem with hash256 in Objective C

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);

(iPhone)URL formatting question

I am trying to format a URL but am getting a bug out of it. My code is below.
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%20score%20is:%i%20and%20CharsPerMin%20is:%#", currentScore, charPerMin.text];
When calling the method it doesn't do a thing. I think the issue is with %20. %20 is being used to space each word in the URL.
You need to escape your % signs by doubling them:
NSString *twitterURL = [NSString stringWithFormat:#"http://twitter.com/?status=My%%20PracticeTyper%%20score%%20is:%i%%20and%%20CharsPerMin%%20is:%#", currentScore, charPerMin.text];