Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何用Cocoa将文本翻译成二进制?_Macos_Cocoa_Binary - Fatal编程技术网

Macos 如何用Cocoa将文本翻译成二进制?

Macos 如何用Cocoa将文本翻译成二进制?,macos,cocoa,binary,Macos,Cocoa,Binary,我正在制作一个简单的Cocoa程序,可以将文本编码为二进制,然后将其解码为文本。我试着写这个剧本,但我还没接近完成。有人能帮我吗?这必须包括两个文本框和两个按钮或任何最好的,谢谢 这有两个部分 第一种方法是将字符串中的字符编码为字节。您可以通过发送字符串adatausingencode:消息来完成此操作。您选择的编码将决定它为每个字符提供的字节数。从NSUTF8StringEncoding开始,然后尝试其他编码,如NSUTF8StringEncoding,一旦它开始工作 第二部分是将每个字节的每

我正在制作一个简单的Cocoa程序,可以将文本编码为二进制,然后将其解码为文本。我试着写这个剧本,但我还没接近完成。有人能帮我吗?这必须包括两个文本框和两个按钮或任何最好的,谢谢

这有两个部分

第一种方法是将字符串中的字符编码为字节。您可以通过发送字符串a
datausingencode:
消息来完成此操作。您选择的编码将决定它为每个字符提供的字节数。从
NSUTF8StringEncoding
开始,然后尝试其他编码,如
NSUTF8StringEncoding
,一旦它开始工作

第二部分是将每个字节的每一位转换为
'0'
字符或
'1'
字符,以便,例如,以UTF-8编码为单个字节的字母a将表示为
01000001

因此,将字符转换为字节,并将字节转换为表示位的字符。这两项任务是完全不同的;第二部分应该可以正确地用于任何字节流,包括任何有效的编码字符流、任何无效的编码字符流,以及任何根本不是文本的内容

第一部分很简单:

- (NSString *) stringOfBitsFromEncoding:(NSStringEncoding)encoding
    ofString:(NSString *)inputString
{
    //Encode the characters to bytes using the UTF-8 encoding. The bytes are contained in an NSData object, which we receive.
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];

    //I did say these were two separate jobs.
    return [self stringOfBitsFromData:data];
}
对于第二部分,需要循环遍历数据的
字节。C
for
循环将在那里完成工作,它将如下所示:

//This is the method we're using above. I'll leave out the method signature and let you fill that in.
- …
{
    //Find out how many bytes the data object contains.
    NSUInteger length = [data length];
    //Get the pointer to those bytes. “const” here means that we promise not to change the values of any of the bytes. (The compiler may give a warning if we don't include this, since we're not allowed to change these bytes anyway.)
    const char *bytes = [data bytes];

    //We'll store the output here. There are 8 bits per byte, and we'll be putting in one character per bit, so we'll tell NSMutableString that it should make room for (the number of bytes times 8) characters.
    NSMutableString *outputString = [NSMutableString stringWithCapacity:length * 8];

    //The loop. We start by initializing i to 0, then increment it (add 1 to it) after each pass. We keep looping as long as i < length; when i >= length, the loop ends.
    for (NSUInteger i = 0; i < length; ++i) {
        char thisByte = bytes[i];

        for (NSUInteger bitNum = 0; bitNum < 8; ++bitNum) {
            //Call a function, which I'll show the definition of in a moment, that will get the value of a bit at a given index within a given character.
            bool bit = getBitAtIndex(thisByte, bitNum);

            //If this bit is a 1, append a '1' character; if it is a 0, append a '0' character.
            [outputString appendFormat: @"%c", bit ? '1' : '0'];
        }
    }

    return outputString;
}
当你小时候学习10进制数字时,你可能注意到10是“10”,100是“100”,以此类推。在2进制中也是如此:10^x是“1”,在10进制中后跟x“0”,所以2^x“1”在2进制中后跟x“0”。例如,基数2中的64是“1000000”(计算零并与上表进行比较)

我们将使用两个数字的精确幂来测试每个输入字节中的每一位

//This is a C function that takes a char and an int, promising not to change either one, and returns a bool.
bool getBitAtIndex(const char byte, const int bitNum)
//It could also be a method, which would look like this:
//- (bool) bitAtIndex:(const int)bitNum inByte:(const char)byte
//but you would have to change the code above. (Feel free to try it both ways.)
{
    //Find 2^bitNum, which will be a number with exactly 1 bit set. For example, when bitNum is 6, this number is “1000000”—a single 1 followed by six 0s—in binary.
    const int powerOfTwo = 1 << bitNum;

    //Test whether the same bit is also set in the input byte.
    bool bitIsSet = byte & powerOfTwo;

    return bitIsSet;
}
找到钻头
C有一对“移位”运算符,可以在数字的低端插入零或删除数字。前者称为“左移”,写为
“文本到二进制”是什么意思?特别是,你所说的“二进制”是什么意思?你是想把每个字符的每个字节的每一位都打印成1或0,还是别的意思?@PeterHosey我想在1和0的每个字节中打印一个简单的文本。我不会把转换成二进制称为“加密”。最多是编码。无论如何,这将帮助您理解所需的数学知识。@PeterHosey仍然没有告诉我如何创建一个可以将文本转换为二进制格式的程序。
  01100101
& 00101011
----------
  00100001
//This is a C function that takes a char and an int, promising not to change either one, and returns a bool.
bool getBitAtIndex(const char byte, const int bitNum)
//It could also be a method, which would look like this:
//- (bool) bitAtIndex:(const int)bitNum inByte:(const char)byte
//but you would have to change the code above. (Feel free to try it both ways.)
{
    //Find 2^bitNum, which will be a number with exactly 1 bit set. For example, when bitNum is 6, this number is “1000000”—a single 1 followed by six 0s—in binary.
    const int powerOfTwo = 1 << bitNum;

    //Test whether the same bit is also set in the input byte.
    bool bitIsSet = byte & powerOfTwo;

    return bitIsSet;
}