creating text from NSWindowsCP1252StringEncoding text - objective-c

I have text below,
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\
\f0\fs24 \cf0 \'83}
In the above text \'83 corresponds to RTF file format and is in NSWindowsCP1252StringEncoding. Now my problem is how to convert to actual representation in
83 corresponds to string ƒ in NSWindowsCP1252StringEncoding.
NSString string with the encoding say using the api
stringWithCString:encoding. I have called the api like below.
NSString* str = [NSString stringWithCString:#"\'83" encoding:NSWindowsCP1252StringEncoding];
but it does not give the text. please let me know how to convert the value to particular text.
Regards,
Lenin

#"\'83" is not a c string, so you shouldn't be passing it to stringWithCString:encoding:. Even a C string version, "\'83", is just ascii characters so using the encoding NSWdindowsCP1252StringEncoding will not produce a string with any special characters.
Instead you need a c string with non-ascii values in order for NSWindowsCP1252StringEncoding to result in a non-ascii character like 'ƒ'.
NSString *str = [NSString stringWithCString:"\x83" encoding:NSWindowsCP1252StringEncoding];

Related

How decode binary string react native

how could I convert a string in binary to string again in react native?
Ex: 01010 to Hello
I have the code to convert string to binary
Ex: text.split('').map(l => l.charCodeAt(0).toString(2)).join(' '),
let txt="Hello".split('').map(l => l.charCodeAt(0).toString(2)).join(' ')
let s = txt.split(" ").map(w=> String.fromCharCode(parseInt(w,2)))
console.log(s.join(""))
Just convert binary string back to integer and map those values to character using fromCharCode
To convert binary string to sting you can use parseInt and String.fromCharCode
parseInt converts strings to number. First argument is string value and the second argument is the base value in which the string representation is.
let num = parseInt(binaryStr, 2);
String.fromCharCode converts character code to matching string.
let str = String.fromCharCode(65);
Complete code with example:
const text = 'Hello'
let binaryStr = text.split('').map(l => l.charCodeAt(0).toString(2)).join(' ');
console.log('Binary string:', binaryStr);
//To convert binary to string
let strVal = binaryStr.split(' ').map(l => String.fromCharCode(parseInt(l, 2))).join('');
console.log('String:', strVal);
Output of above code:
Binary string: 1001000 1100101 1101100 1101100 1101111
String: Hello

Adding back slash for quotes in Swift

I have the following code in Objective-C:
NSString *someString = #"Hello World";
I need a Swift Code with the following output: \"Hello World\"
Including the double checkmarks and the backslash.
According to your comment, you'll need this:
var mySuperFancyDynamicString: String = "whaaaazzuppp"
let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)
print is \"whaaaazzuppp\" (well... was Hello World, before the edit)
Or to your "text box" Comment:
var mySuperFancyDynamicString: String = textBox.text
let someString: String = "\\\"\(mySuperFancyDynamicString)\\\""
print(someString)
Print will be : \"textBoxContent\"
Apparently you want to convert from Objective-c to swift, if yes then don't add backslash to your double quotes, String in swift are just double quoted text.
You should end with
someString = "Hello World"
If You don't want to convert to swift then what are you doing ? Cause the code you are providing (NSString *someString = #\"Hello World\";) will throw an error.
EDIT :
If you really need that specific output, you must create a string like that :
someString = "\\\"Hello World\\\""

Printing newline in TextBlock in windows 8

I want to assign the TextBlock's Text in my code behind and display it on the screen. It might contain new line character also. But somehow the TextBlock is not printing that character. I have used the following combinations in my text to print the new line character
\n
\r\n
Has anyone done this? can you help me?
In XAML you can do like this
<TextBlock>Hello how are you?<LineBreak/>I'm fine</TextBlock>
In code you can do like this
textBlock.Text = "Hello how are you?\nI'm fine.";
Both are working for me.
Edited
For your scenario you can do this
string str = #"Hello how are you?\nI'm fine.";//This is your actual string containing \n as character
or in your case
string str = _arr[index];
str = str.Replace(#"\n", "\n");
Replace "\n" string with new line character.
P.S. It will create problem where you actually want to show \n string instead of new line character.

How do I send multiple querystring parameters in ios?

Here is my code
Passing parameters to my Querystring gives me Bad Access Error!
NSString *myJson = #"http://mySite.com/Service.svc/MyList";
myJson = [myJson stringByAppendingFormat:#"?id=%#&uid=%#", firstId, secondId];
Can someone help me out!
What you do here is simple string formatting.
Given what you are doing, I guess firstId & secondId are integers, not objects, so your error is because you don't use the right format.
The format %# in you stringByAppendingFormat is for displaying an object, or more precisely the string returned by its description selector.
If you want to format an integer, just use %d as in C :)
This will give you :
NSString *myJson = #"http://mySite.com/Service.svc/MyList";
myJson = [myJson stringByAppendingFormat:#"?id=%d&uid=%d", firstId, secondId];

Convert quoted strings in a CSV to an NSMutableArray without the quotes

I have a question about the objective C. I have the following NSString *name shown below:
"First Name","Second Name","Last Name";
Actually, name is the header of the CSV and receive from the URL. And I use the follow statement to break the statement to array.
NSMutableArray *csvTitleArray;
csvTitleArray = [[name componentsSeparatedByString:#","];
The result of the array is
[0] = "First Name" // the " is part of the string, it means the first char of [0] is ", not F
[1] = "Second Name"
[2] = "Last Name"
However, I want to cancel the " in the begin and end of the string (the " is part of the string. Can anyone help me? Thank you.
Have a look at parsing csv data, the General CSV section (code example) handles your case.
See writing parser using nsscanner - csv for usefull more generic pointers about parsing data.
Have you thought of using stringByReplacingOccurrencesOfString:withString: and then splitting the string out into an array?