我是靠谱客的博主 坚定牛排,最近开发中收集的这篇文章主要介绍整型数据转二进制转字符方法(2),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

整型转二进制型转字符型 
#include <iostream>
#include <stdlib.h>

using namespace std;

struct BitFied{
    unsigned char a1:1;
    unsigned char b1:1;
    unsigned char c1:1;
    unsigned char d1:1;
    unsigned char e1:1;
    unsigned char f1:1;
    unsigned char g1:1;
    unsigned char h1:1;

    unsigned char a2:1;
    unsigned char b2:1;
    unsigned char c2:1;
    unsigned char d2:1;
    unsigned char e2:1;
    unsigned char f2:1;
    unsigned char g2:1;
    unsigned char h2:1;

    unsigned char a3:1;
    unsigned char b3:1;
    unsigned char c3:1;
    unsigned char d3:1;
    unsigned char e3:1;
    unsigned char f3:1;
    unsigned char g3:1;
    unsigned char h3:1;

    unsigned char a4:1;
    unsigned char b4:1;
    unsigned char c4:1;
    unsigned char d4:1;
    unsigned char e4:1;
    unsigned char f4:1;
    unsigned char g4:1;
    unsigned char h4:1;
};

union Test{
    struct BitFied binary;
    int iTest;
    unsigned char chTest[4];
};

int main()
{
    Test *test = (Test *)malloc (sizeof (Test));
    memset (test, 0, sizeof (Test));


    /*要转成字符型的整型变量*/
    test->iTest = 6423742723472123;
    cout << "整型型数据" << endl;
    cout << test->iTest << endl; 

    cout << endl;

    /*输出二进制数据*/
    cout << "二进制型数据" << endl;
    printf ("%u", test->binary.h4);
    printf ("%u", test->binary.g4);
    printf ("%u", test->binary.f4);
    printf ("%u", test->binary.e4);
    printf ("%u", test->binary.d4);
    printf ("%u", test->binary.c4);
    printf ("%u", test->binary.b4);
    printf ("%u", test->binary.a4);

    printf ("%u", test->binary.h3);
    printf ("%u", test->binary.g3);
    printf ("%u", test->binary.f3);
    printf ("%u", test->binary.e3);
    printf ("%u", test->binary.d3);
    printf ("%u", test->binary.c3);
    printf ("%u", test->binary.b3);
    printf ("%u", test->binary.a3);

    printf ("%u", test->binary.h2);
    printf ("%u", test->binary.g2);
    printf ("%u", test->binary.f2);
    printf ("%u", test->binary.e2);
    printf ("%u", test->binary.d2);
    printf ("%u", test->binary.c2);
    printf ("%u", test->binary.b2);
    printf ("%u", test->binary.a2);

    printf ("%u", test->binary.h1);
    printf ("%u", test->binary.g1);
    printf ("%u", test->binary.f1);
    printf ("%u", test->binary.e1);
    printf ("%u", test->binary.d1);
    printf ("%u", test->binary.c1);
    printf ("%u", test->binary.b1);
    printf ("%u", test->binary.a1);

    cout << endl;
    cout << endl;

    /*输出字符型的数据*/
    cout << "字符型数据" << endl;
    cout << test->chTest << endl;        

   system ("pause");
    return 0;
}


最后

以上就是坚定牛排为你收集整理的整型数据转二进制转字符方法(2)的全部内容,希望文章能够帮你解决整型数据转二进制转字符方法(2)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部