需求介绍

在编码或者调试过程中经常需要进行 字节码转换为 十六进制的字符串, 或者将 十六进制字符串 转换为 字节码的需求。

即:  字节码 (内存中存储的 01 串):    11111111

<------>

FF

Code

linux上调试通过。

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

// Stringify binary data. Output buffer must be twice as big as input,
// because each byte takes 2 bytes in string representation
static void bin2str(char *to, const unsigned char *p, size_t len) {
  static const char *hex = "0123456789abcdef";

for (; len--; p++) {
    *to++ = hex[p[0] >> 4];
    *to++ = hex[p[0] & 0x0f];
  }
  *to = '\0';
}

unsigned char char2val(const char source)
{
    static const char * hex = "0123456789abcdef";
    size_t index = 0;
    int bLegalChar = 0;

printf("source char =%c\n", source);

for ( index=0 ; index < 16; index++ )
    {
        if ( source == *(hex+index)  )
        {
            bLegalChar = 1;
            break;
        }
    }

if ( !bLegalChar )
    {
        return -1;   
    }

if ( '0' <= source && source <='9'  )
    {
        return (unsigned char) (source - '0');
    }
    else if ( 'a' <= source && source <= 'f' )
    {
        return (unsigned char) (source - 'a') + 10 ;
    }
    else
    {
        return -1;
    }
}

// translate hex string to dest bin buff, which is len in length
static void str2bin(const char* source, unsigned char * dest, size_t len)
{
    unsigned char byte_total = 0;
    unsigned char byte_pre = 0;
    unsigned char byte_post = 0;
    const char *  hex = 0;
    size_t destIndex = 0;

if ( strlen(source) % 2 == 1 )
    {
        printf("!! hex string len error!\n");
        return;
    }

hex = source;
    while ( *(hex) )
    {
        byte_pre = char2val(*hex);
        hex++;

byte_post = char2val(*hex);
        hex++;

byte_total = byte_pre << 4 ;
        printf("byte_total pre << 4 = %d\n", byte_total);

byte_total += byte_post;

printf("byte_pre=%d\n", byte_pre);
        printf("byte_post=%d\n", byte_post);
        printf("byte_total=%d\n", byte_total);

if ( destIndex < len )
        {
            *(dest + destIndex) = byte_total;
            destIndex++;
        }
        else
        {
            break;
        }
    }
}

#define byte unsigned char

#define default_val (unsigned long)-1

void main()
{

char binstr[1024] = {0};
    char binstr2[1024] = {0};

printf("aaaa\n");

byte buff[4] = {0};

byte buff2[4] = {0};

memset(buff, -1, 4);

bin2str(binstr, buff, 4);

printf("buff binstr =%s\n", binstr );

str2bin(binstr, buff2, 4);

bin2str(binstr2, buff2, 4);

printf("buff2 binstr2 =%s\n", binstr2);

printf("bbbb\n");
}

效果

代码演示了, 将一个 4 byte的空间, 初始化为 –1 , 即全部bit位置设置为 1, 然后将此空间转换为 hex字符串,

然后再将此hex字符串转换为 byte字节码空间。

C语言 str2bin 和 bin2str 实现的更多相关文章

  1. C语言 · 高精度加法

    问题描述 输入两个整数a和b,输出这两个整数的和.a和b都不超过100位. 算法描述 由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储.对于这种问题,一般使用数组来处理. 定义一个数组A ...

  2. Windows server 2012 添加中文语言包(英文转为中文)(离线)

    Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...

  3. iOS开发系列--Swift语言

    概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...

  4. C语言 · Anagrams问题

    问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...

  5. C语言 · 字符转对比

    问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...

  6. JAVA语言中的修饰符

    JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...

  7. Atitit 项目语言的选择 java c#.net  php??

    Atitit 项目语言的选择 java c#.net  php?? 1.1. 编程语言与技术,应该使用开放式的目前流行的语言趋势1 1.2. 从个人职业生涯考虑,java优先1 1.3. 从项目实际来 ...

  8. 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】

    说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...

  9. InstallShield 脚本语言学习笔记

    InstallShield脚本语言是类似C语言,利用InstallShield的向导或模板都可以生成基本的脚本程序框架,可以在此基础上按自己的意愿进行修改和添加.     一.基本语法规则      ...

随机推荐

  1. 【BZOJ】1006: [HNOI2008]神奇的国度

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意:在一个弦图中找最少染色数.(n<=10000, m<=1000000) #i ...

  2. [BZOJ2791][Poi2012]Rendezvous

    2791: [Poi2012]Rendezvous Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 95  Solved: 71[Submit][Sta ...

  3. iOS 发布遇到的问题 (转载)

    1.ios图片命名Icon-120.png – 120×120 iphone & ipod touch(ios7)  http://blog.csdn.net/xyxjn/article/de ...

  4. iOS 上线被拒收集

    根据上线被拒的原因 自己 也在慢慢总结  希望对各位有所帮助 1)QQ 微信 等第三方平台 必须要做是否安装应用的检测

  5. 纪念逝去的岁月——C/C++交换排序

    交换排序 代码 #include <stdio.h> void printList(int iList[], int iLen) { ; ; i < iLen; i++) { pri ...

  6. winform学习之-----小知识(20160624)

    一.//判断是否按下回车键if(e.KeyCode == Keys.Enter){   pictureBoxKeyDownLogin_Click(sender,e);}或是e.KeyCode == K ...

  7. Number Sequence

    Number Sequence   A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  8. c#选择填空题题库

    http://wenku.baidu.com/link?url=0g2mfcX_atcRIhJRqJnXzT1s2AIY-a2nR7pUguJn8cdSoy6V0CATevid3eQ7l-kgIDB6 ...

  9. bzoj3991: [SDOI2015]寻宝游戏--DFS序+LCA+set动态维护

    之前貌似在hdu还是poj上写过这道题. #include<stdio.h> #include<string.h> #include<algorithm> #inc ...

  10. html - 自动播放音乐

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...