NSString y intValue
Hola xin com va tot, espero que be, fa temps que no et faig pregunta.
Si vull passar un nsstring a int:
NSString *string = @"123";
int myInt = [string intValue];
NSLog(@" %d ",myInt);
Perfecte pero si el que vull fer es aixo:
NSString *lletrasEnDecimal = [[NSString alloc]init];
NSString *text = [textfield stringValue];
lletrasEnDecimal = [ NSString stringWithFormat:@"%i",[text characterAtIndex:0]];
int numero = [lletrasEnDecimal intValue];
NSLog(@" %d ",numero);
Aixo ya no em funciona em surt per exemple -1073746396 on tindria que ser 2 per exemple.
El que vull fer es passar una lletra al seu valor decimal per despres passarla ha BASE64.
Que en penses.
16 agost 2009 15:11
Té lluís, et faig un regal: 
XCB64Coding.h
/*!
* @file XCB64Coding.h
*
* @updated 2008-11-28
* @copyright Copyright 2008 xin.cat All rights reserved.
*/
#import <Cocoa/Cocoa.h>
#import <openssl/bio.h>
#import <openssl/evp.h>
/*!
* @category NSString_XCB64Coding
* @discussion Category for decode B64/B32 string to binary data
*
* @updated 2008-11-28
*/
@interface NSString (NSString_XCB64Coding)
/*!
* @method
* @abstract Decode B64 string
* @result The data
* @updated 2008-11-28
*/
- (NSData*) decodeB64;
/*!
* @method
* @abstract Decode B32 string
* @result The data
* @updated 2008-11-28
*/
- (NSData*) decodeB32;
/*!
* @method
* @abstract Decode B16 string
* @result The data
* @updated 2008-11-28
*/
- (NSData*) decodeB16;
@end
/*!
* @category NSData_XCB64Coding
* @discussion Category for encode binary data in B64/B32 string
*
* @updated 2008-11-28
*/
@interface NSData (NSData_XCB64Coding)
/*!
* @method
* @abstract Encode de Data to B64 text
* @result The B64 text
* @updated 2008-11-28
*/
- (NSString*) encodeB64;
/*!
* @method
* @abstract Encode de Data to B32 text
* @result The B32 text
* @updated 2008-11-28
*/
- (NSString*) encodeB32;
/*!
* @method
* @abstract Encode de Data to B32 text
* @result The B32 text
* @updated 2008-11-28
*/
- (NSString*) encodeB16;
@end
XCB64Coding.m
/**
* @file XCB64Coding.m
*
* @updated 2008-11-28
* @copyright Copyright 2008 poble.cat All rights reserved.
*/
#import "XCB64Coding.h"
#import "XCHexCoding.h"
@implementation NSString (NSString_XCB64Coding)
- (NSData*) decodeB64
{
void *encodedString = (void*)[self cStringUsingEncoding:NSASCIIStringEncoding];
BIO *mem = BIO_new_mem_buf( encodedString, strlen(encodedString) );
// Push a Base64 filter so that reading from the buffer decodes it
BIO *b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem = BIO_push(b64, mem);
int inlen;
char inbuf[512];
NSMutableData *data = [NSMutableData data];
while ((inlen = BIO_read(mem, inbuf, sizeof(inbuf))) > 0)
[data appendBytes:inbuf length:inlen];
BIO_free_all(mem);
return data;
}
- (NSData*) decodeB32
{
// First valid character that can be indexed in decode lookup table
static int charDigitsBase = '2';
// Lookup table used to decode() characters in encoded strings
static int charDigits[] = {
26,27,28,29,30,31,-1,-1,-1,-1,-1,-1,-1,-1, // 23456789:;<=>?
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, // @ABCDEFGHIJKLMNO
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, // PQRSTUVWXYZ[\]^_
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, // `abcdefghijklmno
15,16,17,18,19,20,21,22,23,24,25 // pqrstuvwxyz
};
if (![self canBeConvertedToEncoding:NSASCIIStringEncoding]) return nil;
NSString *stripEquals = [self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"="]];
const char *chars = [stripEquals cStringUsingEncoding:NSASCIIStringEncoding]; // avoids using characterAtIndex.
int charsLen = [stripEquals length];
// Note that the code below could detect non canonical Base32 length within the loop. However canonical Base32 length can be tested before entering the loop.
// A canonical Base32 length modulo 8 cannot be:
// 1 (aborts discarding 5 bits at STEP n=0 which produces no byte),
// 3 (aborts discarding 7 bits at STEP n=2 which produces no byte),
// 6 (aborts discarding 6 bits at STEP n=1 which produces no byte).
switch (charsLen & 7) { // test the length of last subblock
case 1: // 5 bits in subblock: 0 useful bits but 5 discarded
case 3: // 15 bits in subblock: 8 useful bits but 7 discarded
case 6: // 30 bits in subblock: 24 useful bits but 6 discarded
return nil; // non-canonical length
}
int bytesOffset = 0, charsOffset = 0;
int charDigitsLen = sizeof(charDigits), bytesLen = (charsLen * 5) >> 3;
Byte bytes[bytesLen];
// Also the code below does test that other discarded bits
// (1 to 4 bits at end) are effectively 0.
while (charsLen > 0) {
int digit, lastDigit;
// STEP n = 0: Read the 1st Char in a 8-Chars subblock
// Leave 5 bits, asserting there's another encoding Char
if ((digit = (int)chars[charsOffset] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
lastDigit = digit << 3;
// STEP n = 5: Read the 2nd Char in a 8-Chars subblock
// Insert 3 bits, leave 2 bits, possibly trailing if no more Char
if ((digit = (int)chars[charsOffset + 1] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
bytes[bytesOffset] = (Byte)((digit >> 2) | lastDigit);
lastDigit = (digit & 3) << 6;
if (charsLen == 2) {
if (lastDigit != 0) return nil; // non-canonical end
break; // discard the 2 trailing null bits
}
// STEP n = 2: Read the 3rd Char in a 8-Chars subblock
// Leave 7 bits, asserting there's another encoding Char
if ((digit = (int)chars[charsOffset + 2] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
lastDigit |= (Byte)(digit << 1);
// STEP n = 7: Read the 4th Char in a 8-chars Subblock
// Insert 1 bit, leave 4 bits, possibly trailing if no more Char
if ((digit = (int)chars[charsOffset + 3] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
bytes[bytesOffset + 1] = (Byte)((digit >> 4) | lastDigit);
lastDigit = (Byte)((digit & 15) << 4);
if (charsLen == 4) {
if (lastDigit != 0) return nil; // non-canonical end
break; // discard the 4 trailing null bits
}
// STEP n = 4: Read the 5th Char in a 8-Chars subblock
// Insert 4 bits, leave 1 bit, possibly trailing if no more Char
if ((digit = (int)chars[charsOffset + 4] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
bytes[bytesOffset + 2] = (Byte)((digit >> 1) | lastDigit);
lastDigit = (Byte)((digit & 1) << 7);
if (charsLen == 5) {
if (lastDigit != 0) return nil; // non-canonical end
break; // discard the 1 trailing null bit
}
// STEP n = 1: Read the 6th Char in a 8-Chars subblock
// Leave 6 bits, asserting there's another encoding Char
if ((digit = (int)chars[charsOffset + 5] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
lastDigit |= (Byte)(digit << 2);
// STEP n = 6: Read the 7th Char in a 8-Chars subblock
// Insert 2 bits, leave 3 bits, possibly trailing if no more Char
if ((digit = (int)chars[charsOffset + 6] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
bytes[bytesOffset + 3] = (Byte)((digit >> 3) | lastDigit);
lastDigit = (Byte)((digit & 7) << 5);
if (charsLen == 7) {
if (lastDigit != 0) return nil; // non-canonical end
break; // discard the 3 trailing null bits
}
// STEP n = 3: Read the 8th Char in a 8-Chars subblock
// Insert 5 bits, leave 0 bit, next encoding Char may not exist
if ((digit = (int)chars[charsOffset + 7] - charDigitsBase) < 0 || digit >= charDigitsLen || (digit = charDigits[digit]) == -1)
return nil; // invalid character
bytes[bytesOffset + 4] = (Byte)(digit | lastDigit);
//// This point is always reached for chars.length multiple of 8
charsOffset += 8;
bytesOffset += 5;
charsLen -= 8;
}
// On loop exit, discard the n trailing null bits
return [NSData dataWithBytes:bytes length:sizeof(bytes)];
}
- (NSData*) decodeB16
{
// Variables
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
unsigned int len = [self length] / 2;
unsigned char bytes[len];
unsigned int integer;
NSScanner *scan;
int i;
// Eval each byte from 2 chars
for ( i = 0; i < len; i++ )
{
scan = [NSScanner scannerWithString:[self substringWithRange:NSMakeRange(i*2,2)]];
integer = 0;
[scan scanHexInt:&integer];
bytes[i] = (unsigned char)(integer & 0xFF);
}
// Free memory
[pool release];
// Return data with bytes and len (discart odd chars)
return [NSData dataWithBytes:bytes length:len];
}
@end
@implementation NSData (NSData_XCB64Coding)
- (NSString*) encodeB64
{
BIO *mem = BIO_new(BIO_s_mem());
// Push on a Base64 filter so that writing to the buffer encodes the data
BIO *b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
mem = BIO_push(b64, mem);
BIO_write(mem, [self bytes], [self length]);
BIO_flush(mem);
char *base64Pointer;
long length = BIO_get_mem_data(mem, &base64Pointer);
NSString *base64String = [NSString stringWithCString:base64Pointer length:length];
BIO_free_all(mem);
return base64String;
}
- (NSString*) encodeB32
{
// Lookup table used to canonically encode() groups of data bits
static char canonicalChars[] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M', // 00..12
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', // 13..25
'2','3','4','5','6','7' // 26..31
};
const Byte *bytes = [self bytes];
int bytesOffset = 0, bytesLen = [self length];
int charsOffset = 0, charsLen = ((bytesLen << 3) + 4) / 5;
int addLen = (8 - charsLen) % 8;
char chars[charsLen + addLen];
while (bytesLen != 0) {
int digit, lastDigit;
// INVARIANTS FOR EACH STEP n in [0..5[; digit in [0..31[;
// The remaining n bits are already aligned on top positions
// of the 5 least bits of digit, the other bits are 0.
////// STEP n = 0: insert new 5 bits, leave 3 bits
digit = bytes[bytesOffset] & 255;
chars[charsOffset] = canonicalChars[digit >> 3];
lastDigit = (digit & 7) << 2;
if (bytesLen == 1) { // put the last 3 bits
chars[charsOffset + 1] = canonicalChars[lastDigit];
break;
}
////// STEP n = 3: insert 2 new bits, then 5 bits, leave 1 bit
digit = bytes[bytesOffset + 1] & 255;
chars[charsOffset + 1] = canonicalChars[(digit >> 6) | lastDigit];
chars[charsOffset + 2] = canonicalChars[(digit >> 1) & 31];
lastDigit = (digit & 1) << 4;
if (bytesLen == 2) { // put the last 1 bit
chars[charsOffset + 3] = canonicalChars[lastDigit];
break;
}
////// STEP n = 1: insert 4 new bits, leave 4 bit
digit = bytes[bytesOffset + 2] & 255;
chars[charsOffset + 3] = canonicalChars[(digit >> 4) | lastDigit];
lastDigit = (digit & 15) << 1;
if (bytesLen == 3) { // put the last 1 bits
chars[charsOffset + 4] = canonicalChars[lastDigit];
break;
}
////// STEP n = 4: insert 1 new bit, then 5 bits, leave 2 bits
digit = bytes[bytesOffset + 3] & 255;
chars[charsOffset + 4] = canonicalChars[(digit >> 7) | lastDigit];
chars[charsOffset + 5] = canonicalChars[(digit >> 2) & 31];
lastDigit = (digit & 3) << 3;
if (bytesLen == 4) { // put the last 2 bits
chars[charsOffset + 6] = canonicalChars[lastDigit];
break;
}
////// STEP n = 2: insert 3 new bits, then 5 bits, leave 0 bit
digit = bytes[bytesOffset + 4] & 255;
chars[charsOffset + 6] = canonicalChars[(digit >> 5) | lastDigit];
chars[charsOffset + 7] = canonicalChars[digit & 31];
//// This point is always reached for bytes.length multiple of 5
bytesOffset += 5;
charsOffset += 8;
bytesLen -= 5;
}
// Adding equals
while ( addLen-- ) chars[charsLen+addLen] = '=';
return [NSString stringWithCString:chars length:sizeof(chars)];
}
- (NSString*) encodeB16
{
// Variables
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
unsigned int len = [self length];
NSMutableString *hexStr = [NSMutableString stringWithCapacity:len * 2];
const unsigned char *bytes = [self bytes];
NSString *result;
int i;
// Mount hex text
for ( i = 0; i < len; i++ )
[hexStr appendFormat:@"%02X", bytes[i]];
result = [[NSString alloc] initWithString:hexStr];
// Free memory
[pool release];
return [result autorelease];
}
@end
17 agost 2009 09:21
Ara només cal que agafis un NSString o un NSData i el pordràs codificar i descodificar en B64, B32, B16 (ó hexadecimal).
NSString *text = @"text a codificar";
NSData *data = [text dataUsingEncoding:NSUTF8StringEncoding]
NSString *b64 = [data encodeB64];
NSData *decoded = [b64 decodeB64];
NSString *decodedText = [[NSString alloc] initWithData:decoded];
17 agost 2009 09:29
De totes maneres comentant-te el el codi, hi veig un error:
lletrasEnDecimal = [ NSString stringWithFormat:@"%i",[text characterAtIndex:0]];
int numero = [lletrasEnDecimal intValue];
NSLog(@" %d ",numero);
El mètode "characterAtIndex:" retorna un "unichar" enter curt (16bits) sense signe. I després tu el passes a string per tornar-lo a traduir a enter. Amb el primer valor ja n'hauries de tenir prou.
Si de totes formes ho vols fer utilitza el format "%u" que són per als enters sense signe, sinó els valors majoris de 32000 sortiran negatius.
17 agost 2009 09:43
Moltes gracies per tot, em tinc que estudiar el codi per entendre be com funciona per que algunes parts semblen complicades, pero aixo va be per apendre.
17 agost 2009 21:58
Jo no m'hi mataria massa.
- L'encodeB64 funciona amb les llibreries OpenSSL del sistema i poca cosa podràs trobar-hi.
- L'únic amb quelcom interessant és el encodeB32 que vaig treure d'Internet i no he perdut massa temps en comprovar com funciona.
- El encodeB16 és l'únic que he fet jo i simplement codifica en hexadecimal, així que simplement tradueix un byte en el seu valor hexadecimal.
L'únic interessant són, si no les coneixies, les "Categories" que permeten afegir funcionalitat a classes existents sempre que no s'hagin d'afegir noves variables d'instància.
18 agost 2009 08:00
