Convert Hex String to ASCII Format [duplizieren]
Diese Frage hat hier bereits eine Antwort:
NSString mit Hex konvertieren zu ASCII-Äquivalent 1 AntwortIch habe eine Hex-Zeichenfolge wie "000000000100" und verwende die folgende Logik, um die ASCII-Konvertierung durchzuführen. Die Ausgabe, die ich empfange, ist nur 1 Byte (\ x01). Ich möchte jedoch die Ausgabe im 6-Byte-Format als \ x00 \ x00 \ x00 \ x00 \ x01 \ x00
-(NSString*) decode
{
string=@"000000000100";
NSMutableString * newString = [[NSMutableString alloc]init];
int i = 0;
while (i < [string length])
{
NSString * hexChar = [string substringWithRange: NSMakeRange(i, 2)];
int value = 0;
sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
[newString appendFormat:@"%c", (char)value];
i+=2;
}
return newString;
}
Wie geht das