需求介绍

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

即:  字节码 (内存中存储的 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. Issues I encountered when building Windows Store apps on a new laptop

    I took over my beloved wives samsung ativ book 9 recently as her first job granted her a brandnew su ...

  2. HDU - Travel

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  3. 【Java】RuleSource约束常用方法整理

    1-常用约束规则RuleSource的设置方法   例如: addRules(new Rules(ProgramFeeItem.class){ protected void initRules() { ...

  4. vim 使用笔记

    vim命令笔记 a 插入 insert 插入 :%!xxd 以16进制方式进行编辑 :%!xxd -r 从16进制还原

  5. Handler消息传递机制

    引言: 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题. 为了解决这个问题,Android制定了一条简单的规则:只允许UI线程 ...

  6. maven创建web工程,并导入到eclipse中

    maven版本:3.1.0,eclipse版本:4.3 JDK:1.7 创建工程名是sa的maven工程 mvn archetype:create -DgroupId=com.bing-Dartifa ...

  7. AlertDialog使用时遇到问题

    1.其内de子控件高宽度为0? AlertDialog.Builder builder = new AlertDialog.Builder(StoryActivity.this); View view ...

  8. [LintCode] Length of Last Word 求末尾单词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. Top Deep Learning Projects in github

    Top Deep Learning Projects A list of popular github projects related to deep learning (ranked by sta ...

  10. FK JavaScript之:ArcGIS JavaScript添加Graphic,地图界面却不显示

    使用ArcGIS JavaScript,往地图中添加几个Graphic,基本是与官网示例代码一致.绘制的图形一闪而过之后,就没了 核心代码如下: iniToolBar: function () { t ...