0

Hey i'm trying to get text to ASCII and then store it on a server then when i get it back i want to decode the text back to NSString

i've used the following code to test on a random app.. but in here i have NSdata as a global variable so it works fine..

Question is how do i get the value NSString i get back from the server in NSData variable so i can convert it back to the text i encoded from.

.h

@interface passTestViewController : UIViewController
{
    NSString *text;
    NSData *d;
}

.m

- (IBAction)eButton:(id)sender {
    text = [NSString stringWithFormat:@"%@%@%@",@"!",[_textField text],@"!"];

    d=[[NSString stringWithFormat:@"%@",text] dataUsingEncoding:NSASCIIStringEncoding];
    text = [NSString stringWithFormat:@"%@",d];

    [_label setText:text];
}

- (IBAction)dButton:(id)sender {

    NSString *dString = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];
    dString = [dString stringByReplacingOccurrencesOfString:@"!" withString:@""];
    [_label setText:dString];
}

i need to get it to NSData. i send it to my server thru JSON and then later when i get it back... i get it as a string.. what i need is

NSData *d = stringThatIGetBackFromJson; 

so i can convert it thru

NSString *dString = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding];

to get the original string entered by user.

Also the string i get from this after encode has the signs and spaces.. '<' '>' and ' ' It wouldn't go thru to the url so what i did was remove the spaces and < > thru

myString = [myString stringByReplacingOccurrencesOfString:@" " withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@"<" withString:@""];
myString = [myString stringByReplacingOccurrencesOfString:@">" withString:@""];

so now what i'll get back is gonna be just numbers like "40786d61646f6f2131323334216b6d646f616f"... is there anyway around this ? to compare i'm just checking encoding the text and following the same steps and then comparing if isEqualTo, but i would like to get the text back to be able to post on the app.

Amorn Narula
  • 323
  • 3
  • 13
  • I'm afraid you're hopelessly confused. You apparently don't understand that "encoding" only applies to 8-bit data -- once a string is in an NSString it's in Unicode. You also don't understand that if you dump (or format with `%@`) an NSData object you'll see hex, not character data. This is true of ***any*** NSData object. (The above hex is "@xmadoo!1234!kmdoao") – Hot Licks Nov 02 '12 at 12:29
  • So i can just use a normal hex to string convertor to convert it back..e.g: http://stackoverflow.com/questions/6421282/how-to-convert-hex-to-nsstring-in-objective-c . and is there a way that i can send as data.. ? or any other option for encryption? Thnx and sry for the late reply.. – Amorn Narula Nov 27 '12 at 09:33
  • Use NSString initWithData... You should use the same string encoding that was used to produce the data originally, but UTF8 should work in almost all cases. – Hot Licks Nov 27 '12 at 13:18
  • (You are hopelessly confused. If you don't have someone you can discuss this with locally, start with a *very simple* test case and slowly add more features until you figure it out.) – Hot Licks Nov 27 '12 at 13:20
  • (And next time you tell us something "didn't work" tell us precisely what happened!) – Hot Licks Nov 27 '12 at 13:21

2 Answers2

6

Try the below three ways to convert NSString to NSData

1.NSData* data = [yourString dataUsingEncoding:NSUTF8StringEncoding];

2.NSData* data=[yourString dataUsingEncoding: [NSString defaultCStringEncoding] ];

3.NSData* data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
data = [data subdataWithRange:NSMakeRange(0, [data length] - 1)];

If the data is not null-terminated,

NSString* myString = [[NSString alloc] initWithData:nsDataString
                                         encoding:NSUTF8StringEncoding] ;

If the data is null-terminated

NSString* myString = [NSString stringWithUTF8String:[nsDataString bytes]];
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • i need to get it to NSData. i send it to my server thru JSON and then later when i get it back... i get it as a string.. what i need is NSData *d = stringThatIGetBackFromJson; so i can convert it thru NSString *dString = [[NSString alloc] initWithData:d encoding:NSASCIIStringEncoding]; to get the original string entered by user. – Amorn Narula Nov 02 '12 at 10:08
0

try to change NSASCIIStringEncoding to NSUTF8StringEncoding

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
user1203650
  • 300
  • 2
  • 3