这是一个简单的程序:
二进制到字符串转换器
我仍然是编程的新手,我真的很感激帮助
我只需要一个在Xcode中编译和运行的小脚本
所有程序要做的就是转换一个二进制字符串并显示隐藏的单词/消息。全部是
例如
0110001001110010011010010110000101101110001100010011001000110011 ==“Brian123”
或者喜欢这个网站Binary to Ascii Text Converter
http://www.binaryhexconverter.com/binary-to-ascii-text-converter
的 的 ** * ** * ** * 的** * ** 我的代码很远 * ** * ** * ** * ** * *
- (IBAction)ConvertButton:(id)sender{
NSString* makelink =[[NSString alloc]initWithFormat:@"%@",[_CharacterInput text]];
NSDictionary *BinaryConverter=[NSDictionary dictionaryWithObjectsAndKeys:
////number 0-9///////////////
@"0011 0000 ",@"0",
@"0011 0001 ",@"1",
@"0011 0010 ",@"2",
@"0011 0011 ",@"3",
@"0011 0100 ",@"4",
@"0011 0101 ",@"5",
@"0011 0110 ",@"6",
@"0011 0111 ",@"7",
@"0011 1000 ",@"8",
@"0011 1001 ",@"9",
/////Small letters//////////////
@"0110 0001 ",@"a",
@"0110 0010 ",@"b",
@"0110 0011 ",@"c",
@"0110 0100 ",@"d",
@"0110 0101 ",@"e",
@"0110 0110 ",@"f",
@"0110 0111 ",@"g",
@"0110 1000 ",@"h",
@"0110 1001 ",@"i",
@"0110 1010 ",@"j",
@"0110 1011 ",@"k",
@"0110 1100 ",@"l",
@"0110 1101 ",@"m",
@"0110 1110 ",@"n",
@"0110 1111 ",@"o",
@"0111 0000 ",@"p",
@"0111 0001 ",@"q",
@"0111 0010 ",@"r",
@"0111 0011 ",@"s",
@"0111 0100 ",@"t",
@"0111 0101 ",@"u",
@"0111 0110 ",@"v",
@"0111 0111 ",@"w",
@"0111 1000 ",@"x",
@"0111 1001 ",@"y",
@"0111 1010 ",@"z",
//CAPITAL LETTERS/////////////////////
@"0100 0001 ",@"A",
@"0100 0010 ",@"B",
@"0100 0011 ",@"C",
@"0100 0100 ",@"D",
@"0100 0101 ",@"E",
@"0100 0110 ",@"F",
@"0100 0111 ",@"G",
@"0100 1000 ",@"H",
@"0100 1001 ",@"I",
@"0100 1010 ",@"J",
@"0100 1011 ",@"K",
@"0100 1100 ",@"L",
@"0100 1101 ",@"M",
@"0100 1110 ",@"N",
@"0100 1111 ",@"O",
@"0101 0000 ",@"P",
@"0101 0001 ",@"Q",
@"0101 0010 ",@"R",
@"0101 0011 ",@"S",
@"0101 0100 ",@"T",
@"0101 0101 ",@"U",
@"0101 0110 ",@"V",
@"0101 0111 ",@"W",
@"0101 1000 ",@"X",
@"0101 1001 ",@"Y",
@"0101 1010 ",@"Z",
nil];
//////////// number 0-9///////////////
NSString * Number0 = @"0";
NSString * Number1 = @"1";
NSString * Number2 = @"2";
NSString * Number3 = @"3";
NSString * Number4 = @"4";
NSString * Number5 = @"5";
NSString * Number6 = @"6";
NSString * Number7 = @"7";
NSString * Number8 = @"8";
NSString * Number9 = @"9";
////////////Small Letterrs///////////////
NSString * Lettera = @"a";
NSString * Letterb = @"b";
NSString * Letterc = @"c";
NSString * Letterd = @"d";
NSString * Lettere = @"e";
NSString * Letterf = @"f";
NSString * Letterg = @"g";
NSString * Letterh = @"h";
NSString * Letteri = @"i";
NSString * Letterj = @"j";
NSString * Letterk = @"k";
NSString * Letterl = @"l";
NSString * Letterm = @"m";
NSString * Lettern = @"n";
NSString * Lettero = @"o";
NSString * Letterp = @"p";
NSString * Letterq = @"q";
NSString * Letterr = @"r";
NSString * Letters = @"s";
NSString * Lettert = @"t";
NSString * Letteru = @"u";
NSString * Letterv = @"v";
NSString * Letterw = @"w";
NSString * Letterx = @"x";
NSString * Lettery = @"y";
NSString * Letterz = @"z";
////////////Capital Letters///////////////
NSString * LetterA = @"A";
NSString * LetterB = @"B";
NSString * LetterC = @"C";
NSString * LetterD = @"D";
NSString * LetterE = @"E";
NSString * LetterF = @"F";
NSString * LetterG = @"G";
NSString * LetterH = @"H";
NSString * LetterI = @"I";
NSString * LetterJ = @"J";
NSString * LetterK = @"K";
NSString * LetterL = @"L";
NSString * LetterM = @"M";
NSString * LetterN = @"N";
NSString * LetterO = @"O";
NSString * LetterP = @"P";
NSString * LetterQ = @"Q";
NSString * LetterR = @"R";
NSString * LetterS = @"S";
NSString * LetterT = @"T";
NSString * LetterU = @"U";
NSString * LetterV = @"V";
NSString * LetterW = @"W";
NSString * LetterX = @"X";
NSString * LetterY = @"Y";
NSString * LetterZ = @"Z";
/////////////Numbers 0-9///////////////////////
if ([_CharacterInput.text isEqual: @"0"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number0]];
}
if ([_CharacterInput.text isEqual: @"1"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number1]];
}
if ([_CharacterInput.text isEqual: @"2"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number2]];
}
if ([_CharacterInput.text isEqual: @"3"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number3]];
}
if ([_CharacterInput.text isEqual: @"4"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number4]];
}
if ([_CharacterInput.text isEqual: @"5"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number5]];
}
if ([_CharacterInput.text isEqual: @"6"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number6]];
}
if ([_CharacterInput.text isEqual: @"7"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number7]];
}
if ([_CharacterInput.text isEqual: @"8"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number8]];
}
if ([_CharacterInput.text isEqual: @"9"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Number9]];
}
///////////Small Letters////////////////////
if ([_CharacterInput.text isEqual: @"a"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettera]];
}
if ([_CharacterInput.text isEqual: @"b"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterb]];
}
if ([_CharacterInput.text isEqual: @"c"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterc]];
}
if ([_CharacterInput.text isEqual: @"d"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterd]];
}
if ([_CharacterInput.text isEqual: @"e"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettere]];
}
if ([_CharacterInput.text isEqual: @"f"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterf]];
}
if ([_CharacterInput.text isEqual: @"g"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterg]];
}
if ([_CharacterInput.text isEqual: @"h"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterh]];
}
if ([_CharacterInput.text isEqual: @"i"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letteri]];
}
if ([_CharacterInput.text isEqual: @"j"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterj]];
}
if ([_CharacterInput.text isEqual: @"k"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterk]];
}
if ([_CharacterInput.text isEqual: @"l"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterl]];
}
if ([_CharacterInput.text isEqual: @"m"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterm]];
}
if ([_CharacterInput.text isEqual: @"n"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettern]];
}
if ([_CharacterInput.text isEqual: @"o"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettero]];
}
if ([_CharacterInput.text isEqual: @"p"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterp]];
}
if ([_CharacterInput.text isEqual: @"q"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterq]];
}
if ([_CharacterInput.text isEqual: @"r"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterr]];
}
if ([_CharacterInput.text isEqual: @"s"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letters]];
}
if ([_CharacterInput.text isEqual: @"t"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettert]];
}
if ([_CharacterInput.text isEqual: @"u"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letteru]];
}
if ([_CharacterInput.text isEqual: @"v"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterv]];
}
if ([_CharacterInput.text isEqual: @"w"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterw]];
}
if ([_CharacterInput.text isEqual: @"x"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterx]];
}
if ([_CharacterInput.text isEqual: @"y"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Lettery]];
}
if ([_CharacterInput.text isEqual: @"z"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:Letterz]];
}
//////////// large Caps//////////////////////////////////////
if ([_CharacterInput.text isEqual: @"A"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterA]];
}
if ([_CharacterInput.text isEqual: @"B"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterB]];
}
if ([_CharacterInput.text isEqual: @"C"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterC]];
}
if ([_CharacterInput.text isEqual: @"D"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterD]];
}
if ([_CharacterInput.text isEqual: @"E"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterE]];
}
if ([_CharacterInput.text isEqual: @"F"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterF]];
}
if ([_CharacterInput.text isEqual: @"G"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterG]];
}
if ([_CharacterInput.text isEqual: @"H"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterH]];
}
if ([_CharacterInput.text isEqual: @"I"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterI]];
}
if ([_CharacterInput.text isEqual: @"J"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterJ]];
}
if ([_CharacterInput.text isEqual: @"K"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterK]];
}
if ([_CharacterInput.text isEqual: @"L"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterL]];
}
if ([_CharacterInput.text isEqual: @"M"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterM]];
}
if ([_CharacterInput.text isEqual: @"N"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterN]];
}
if ([_CharacterInput.text isEqual: @"O"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterO]];
}
if ([_CharacterInput.text isEqual: @"P"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterP]];
}
if ([_CharacterInput.text isEqual: @"Q"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterQ]];
}
if ([_CharacterInput.text isEqual: @"R"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterR]];
}
if ([_CharacterInput.text isEqual: @"S"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterS]];
}
if ([_CharacterInput.text isEqual: @"T"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterT]];
}
if ([_CharacterInput.text isEqual: @"U"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterU]];
}
if ([_CharacterInput.text isEqual: @"V"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterV]];
}
if ([_CharacterInput.text isEqual: @"W"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterW]];
}
if ([_CharacterInput.text isEqual: @"X"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterX]];
}
if ([_CharacterInput.text isEqual: @"Y"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterY]];
}
if ([_CharacterInput.text isEqual: @"Z"]) {
[_BinaryDataTextField setText:[BinaryConverter objectForKey:LetterZ]];
}
}
- (IBAction)RemoveKeyboard:(id)sender{
[_CharacterInput resignFirstResponder];
[_BinaryDataTextField resignFirstResponder];
}
@end
答案 0 :(得分:5)
你去吧
NSString *str = @"01100010 01110010 01101001 01100001 01101110 00110001 00110010 00110011";
NSString *str2 = stringFromBinString(str);
NSLog(@"%@",str2); // brian123
这是可以转换的功能
// Function that converts
NSString* stringFromBinString(NSString* binString) {
NSArray *tokens = [binString componentsSeparatedByString:@" "];
char *chars = malloc(sizeof(char) * ([tokens count] + 1));
for (int i = 0; i < [tokens count]; i++) {
const char *token_c = [[tokens objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding];
char val = (char)strtol(token_c, NULL, 2);
chars[i] = val;
}
chars[[tokens count]] = 0;
NSString *result = [NSString stringWithCString:chars
encoding:NSUTF8StringEncoding];
free(chars);
return result;
}
这是完整的节目,因为我今天心情很好
//
// main.m
// HelpForBrianTheProgrammer
//
// Created by Filip on 2013-06-25.
#import <Foundation/Foundation.h>
NSString* stringFromBinString(NSString* binString) {
NSArray *tokens = [binString componentsSeparatedByString:@" "];
char *chars = malloc(sizeof(char) * ([tokens count] + 1));
for (int i = 0; i < [tokens count]; i++) {
const char *token_c = [[tokens objectAtIndex:i] cStringUsingEncoding:NSUTF8StringEncoding];
char val = (char)strtol(token_c, NULL, 2);
chars[i] = val;
}
chars[[tokens count]] = 0;
NSString *result = [NSString stringWithCString:chars
encoding:NSUTF8StringEncoding];
free(chars);
return result;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSString *binString = @"0110001001110010011010010110000101101110001100010011001000110011";
// Splitting the string into an array
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:binString.length];
for (NSUInteger i = 0; i < binString.length; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [binString characterAtIndex:i]];
[characters addObject:ichar];
}
NSMutableString *str = [NSMutableString new];
NSLog(@"%@",str);
NSInteger counter = 0;
// Splitting the array into a string of blocks of 8 charachters with a space inbetween
for (NSString *strPart in characters) {
[str appendString:strPart];
counter ++;
if(counter > 0 && counter%8 == 0)
[str appendString:@" "];
}
NSLog(@"%@",str);
NSString *theString = stringFromBinString(str);
NSLog(@"%@",theString);
}
return 0;
}
只是为了澄清;此代码可以正常工作并获取以下日志
2013-06-25 13:35:49.525 HelpForBrianTheProgrammer [16459:303] 01100010 01110010 01101001 01100001 01101110 00110001 00110010 00110011
2013-06-25 13:35:49.527 HelpForBrianTheProgrammer [16459:303] brian123