C语言 str2bin 和 bin2str 实现
需求介绍
在编码或者调试过程中经常需要进行 字节码转换为 十六进制的字符串, 或者将 十六进制字符串 转换为 字节码的需求。
即: 字节码 (内存中存储的 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 实现的更多相关文章
- C语言 · 高精度加法
问题描述 输入两个整数a和b,输出这两个整数的和.a和b都不超过100位. 算法描述 由于a和b都比较大,所以不能直接使用语言中的标准数据类型来存储.对于这种问题,一般使用数组来处理. 定义一个数组A ...
- Windows server 2012 添加中文语言包(英文转为中文)(离线)
Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...
- iOS开发系列--Swift语言
概述 Swift是苹果2014年推出的全新的编程语言,它继承了C语言.ObjC的特性,且克服了C语言的兼容性问题.Swift发展过程中不仅保留了ObjC很多语法特性,它也借鉴了多种现代化语言的特点,在 ...
- C语言 · Anagrams问题
问题描述 Anagrams指的是具有如下特性的两个单词:在这两个单词当中,每一个英文字母(不区分大小写)所出现的次数都是相同的.例如,"Unclear"和"Nuclear ...
- C语言 · 字符转对比
问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...
- JAVA语言中的修饰符
JAVA语言中的修饰符 -----------------------------------------------01--------------------------------------- ...
- Atitit 项目语言的选择 java c#.net php??
Atitit 项目语言的选择 java c#.net php?? 1.1. 编程语言与技术,应该使用开放式的目前流行的语言趋势1 1.2. 从个人职业生涯考虑,java优先1 1.3. 从项目实际来 ...
- 【开源】简单4步搞定QQ登录,无需什么代码功底【无语言界限】
说17号发超简单的教程就17号,qq核审通过后就封装了这个,现在放出来~~ 这个是我封装的一个开源项目:https://github.com/dunitian/LoTQQLogin ————————— ...
- InstallShield 脚本语言学习笔记
InstallShield脚本语言是类似C语言,利用InstallShield的向导或模板都可以生成基本的脚本程序框架,可以在此基础上按自己的意愿进行修改和添加. 一.基本语法规则 ...
随机推荐
- UVA 11461 - Square Numbers(水题)
题目链接 #include <cstdio> #include <cstring> #include <string> #include <cmath> ...
- 【bzoj2179】FFT快速傅立叶 FFT模板
2016-06-01 09:34:54 很久很久很久以前写的了... 今天又比较了一下效率,貌似手写复数要快很多. 贴一下模板: #include<iostream> #include& ...
- 不同版本vpb与osg对应关系
不同版本vpb与osg对应关系 转自:http://blog.sina.com.cn/s/blog_668aae780101k6pr.html VirtualPlanetBuilder是一种地形数据库 ...
- NodeJS学习笔记之Connect中间件模块(一)
NodeJS学习笔记之Connect中间件模块(一) http://www.jb51.net/article/60430.htm NodeJS学习笔记之Connect中间件模块(二) http://w ...
- java web(一) 使用sql标签库+tomcat+mysql手动创建一个jsp练习总结
2016-09-0111:06:53 使用sql标签库+tomcat+mysql手动创建一个jsp 1. 1.1安装tomcat ...
- [转载]对于GetBuffer() 与 ReleaseBuffer() 的一些分析
先 转载一段别人的文章 CString类的这几个函数, 一直在用, 但总感觉理解的不够透彻, 不时还有用错的现象. 今天抽时间和Nico一起分析了一下, 算是拨开了云雾: GetBuffer和Rele ...
- div居中
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- iOS Core Animation
1.什么是Core Animation? 它是一套包含图形绘制,投影,动画的OC类集合.它就是一个framework.通过CoreAnimation提供的接口,你可以方便完成自己所想要的动画. 2.我 ...
- NeuSoft(1)构建嵌入式交叉编译环境
操作系统版本:ubuntu 12.04 内核名称:Linux 内核发行版:3.2.0-generic 内核版本:#50-Ubuntu SMP Mon Sep 12 21:18:14 UTC 2011 ...
- MySQL执行存储过程权限
http://url.cn/f2bj78 MySQL grant不能在on后面写多个对象