ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据转换-位串字节数组

2022-06-02 14:33:54  阅读:131  来源: 互联网

标签:字节 int 位串 ret char 数组 printf include testdi


数据转换-位串字节数组

  1. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务

1 参考《GMT 0009-2012 SM2密码算法使用规范》第6节“数据转换” 在utils.h和utils.c中完成位串与8位字节串的转换功能(10'):
int Bitstr2ByteArr(unsigned char * bs, unsigned char * ba,int *lba);
int ByteArr2Bitstr(unsigned char * ba, unsigned char * bs, int *lbs);

2 并写出测试代码测试上述函数(不能与下面代码一样),比如(10'):

unsigned char bs [] = "1010010100011100";
int len;
Bitstr2ByteArr(bs, char * ba, &len);
//结果:ba = {0x5, 0x1C}; len=2

char ba[] = {0x5, 0x1C}
ByteArr2Bitstr(char * ba, char * bs);
//结果:bs= "1010010100011100"
3 提交代码(或代码链接)和运行结果


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

int main(){

    void ** pdh;
    pdh = (void **) malloc(20);
    int ret;

    ret = SDF_OpenDevice(pdh);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("device opened!\n");
    }

    DEVICEINFO testdi;
    ret = SDF_GetDeviceInfo(pdh, &testdi);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("Issuer Name: %s\n", testdi.IssuerName);
        printf("Device Name: %s\n", testdi.DeviceName);
        printf("Device Serial: %s\n", testdi.DeviceSerial);
        printf("Device Version: %d\n", testdi.DeviceVersion);

    }

    char pRandom[10];

    ret = SDF_GenerateRandom(*pdh,10, pRandom);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        for(int i=0; i<10; i++)
            printf("%d\n", pRandom[i]);
    }


    ret = SDF_CloseDevice(*pdh);

    if(ret != SDR_OK){
        printf("error!");
    } else {
        free(pdh);
        printf("device closed!\n");
    }

    return 0;
}
#include "sdf.h"
#include <stdio.h>
#include <stdlib.h>

int main(){

    void ** pdh;
    pdh = (void **) malloc(20);
    int ret;

    ret = SDF_OpenDevice(pdh);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("device opened!\n");
    }

    DEVICEINFO testdi;
    ret = SDF_GetDeviceInfo(pdh, &testdi);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        printf("Issuer Name: %s\n", testdi.IssuerName);
        printf("Device Name: %s\n", testdi.DeviceName);
        printf("Device Serial: %s\n", testdi.DeviceSerial);
        printf("Device Version: %d\n", testdi.DeviceVersion);

    }

    char pRandom[10];

    ret = SDF_GenerateRandom(*pdh,10, pRandom);
    if(ret != SDR_OK){
        printf("error!");
    } else {
        for(int i=0; i<10; i++)
            printf("%d\n", pRandom[i]);
    }


    ret = SDF_CloseDevice(*pdh);

    if(ret != SDR_OK){
        printf("error!");
    } else {
        free(pdh);
        printf("device closed!\n");
    }

    return 0;
}
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>

//********************************
//设备管理
//********************************

int SDF_OpenDevice(void ** phDeviceHandle){
    return SDR_OK;
}


int SDF_CloseDevice(void *hDeviceHandle){

    return SDR_OK;
}

int SDF_GetDeviceInfo( void * hSessionHandle, DEVICEINFO * pstDeviceInfo) {

    DEVICEINFO di;
    strcpy(di.IssuerName,"RocSDF");
    strcpy(di.DeviceName,"SDFBESTI181x");
    strcpy(di.DeviceSerial,"2021040001");
    di.DeviceVersion = 1;
    //...

    //pstDevicelnfo = &di;
    *pstDeviceInfo = di;

    return SDR_OK;
}

static int myRandom(){
    srand((unsigned)time(NULL));
    return rand();
}

int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength, unsigned char * pucRandom){


    for(int i=0; i<uiLength; i++){
        sleep(2);
        *(pucRandom+i) = myRandom();
       // pucRandom[i] = i;
    }

    return SDR_OK;
}
#include <stdio.h>
#include "util.h"

char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char Hex2Char(int i){
/*
    if(i>=0 && i<= 9)
        return i + 0x30;
        //return i + '0'
    if(i>=10 && i<=15)
        return i + 0x37;
       // return  i + 'A' - 10;
*/
    return HStr[i];
}

对我的学号进行转换:

标签:字节,int,位串,ret,char,数组,printf,include,testdi
来源: https://www.cnblogs.com/GanNy/p/16337628.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有