【问题标题】:What is this part of the code doing?这部分代码在做什么?
【发布时间】:2013-08-22 05:45:56
【问题描述】:

我是 C 编程新手。作为网络安全课程的一部分,我必须设计一个 SSL 握手模拟。我在网上找到了一个示例代码,但是我不明白代码的某些部分。您能帮我解决以下问题吗:

(char) 0 是做什么的?? ( send_data 定义为char send_data[1024]; )

send_data[0] = (char) 0;                //Packet Type = hello
send_data[1] = (char) 3;                //Version

编辑+跟进

伙计们,我知道什么是类型转换。

我明白什么是强制转换但我发布的代码什么也没做。即使整数 0 被转换为字符,它也没有做任何事情,因为当你打印它时 - 它是一个空白 - 没有值。

例如:

#include <stdio.h>
#include <stdlib.h>

int main(){

char test;
int num;

num = 1;
test = (char) num; // this does nothing

printf("num = %d , %c\n",num,num);
printf("test = %d , %c\n",test,test);

    // Isn't this the correct way to do it ?? :

num = 3;
test = '3'; // now this is a character 3

printf("num = %d , %c\n",num,num);
printf("test = %d , %c\n",test,test);

return 0;

}

上面代码的输出是:

num = 1 , 
test = 1 , 
num = 3 , 
test = 51 , 3

那么为什么要这样做呢?这不是正确的方法吗:- send_data[0] = '0';发送数据[1] = '3';

【问题讨论】:

  • 听说过选角吗?
  • 请阅读 K&R 的“C语言编程”一书

标签: c sockets networking ssl network-programming


【解决方案1】:

它只是将int 0(或3)转换为char 类型。

这可能不是必需的,但可用于删除可能截断的警告。

更好的成语是:

send_data[0] = '\x00';   // Packet Type = hello
send_data[1] = '\x03';   // Version

因为这些都是明确的字符,所以不必担心转换。

请记住,(char) 0(或'\x00''0' 相同。前两个为您提供字符代码 0(ASCII 中的 NUL 字符),后者为您提供 printable 0 字符的字符代码(ASCII 中的字符代码 48 或 '\x30' )。 这就是为什么您的打印效果不如您预期的原因。

您的特定 协议是否需要代码点 0 或可打印字符 0 是您尚未明确的。如果您真的想模拟 SSLv3,那么正确的值是二进制而不是 RFC6101 中的可打印值:

enum {
    hello_request(0), client_hello(1), server_hello(2),
    certificate(11), server_key_exchange (12),
    certificate_request(13), server_done(14),
    certificate_verify(15), client_key_exchange(16),
    finished(20), (255)
} HandshakeType;

【讨论】:

    【解决方案2】:
    int main() 
    { 
        char ch;
        ch = (char) 0;
        printf("%d\n", ch);   //This will print 0
        printf("%c\n", ch);   //This will print nothing (character whose value is 0 which is NUL)
        ch = (char) 3;
        printf("%d\n", ch);   //This will print 3
        printf("%c\n", ch);   //This will print a character whose value is 3
        return 0;
    }
    

    将int类型转换为char类型。

        Its good to create a demo program and test it when you get some doubts while reading.
    

    【讨论】:

    • s/这将不打印任何内容/这将打印一个值为 0/ 的字符 - 这些代码段之间根本没有区别,都只输出一个字符(代码点 0 或 3)。
    • 我明白什么是强制转换但我发布的代码什么也没做。即使整数 0 被强制转换为字符,它也不会执行任何操作,因为就像在您的示例中一样,当您打印它时 - 它是一个空白 - 没有任何价值。那为什么要做呢??这不是正确的方法吗:- send_data[0] = '0';发送数据[1] = '3';
    【解决方案3】:

    它只是将文字符号转换为 char 值。但我认为没有必要。

    【讨论】:

      【解决方案4】:

      send_data 定义为char 的数组。但是03 是整数文字。当整数分配给数组时,它们被转换为char。这意味着 send_data [0] 将保存 ASCII 值为 0 的字符,即 NUL 字符。 send_data[1] 将保存 ASCII 值为 3 的字符,即end of text 字符。

      【讨论】:

        【解决方案5】:

        这称为type-conversioncasting。当您需要将一种数据类型的实体更改为另一种时,您可以这样做。

        在您的示例中,0 和 3(整数)被转换为字符类型。

        http://en.wikipedia.org/wiki/Type_conversion

        【讨论】:

          【解决方案6】:

          castingint 值转换为char 类型。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-11-30
            • 1970-01-01
            • 2014-05-01
            • 1970-01-01
            • 2016-05-31
            • 2010-10-03
            • 2018-11-01
            • 1970-01-01
            相关资源
            最近更新 更多