处理从和到十六进制的转换 [英] Handling of conversions from and to hex

查看:125
本文介绍了处理从和到十六进制的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要构建一个函数来轻松地将包含十六进制代码的字符串(例如0ae34e)转换为包含等效ascii值的字符串,反之亦然。
我是否需要将Hex字符串分成两对值并将它们再次联系在一起,或者是否有一种方便的方法来实现这一点?

谢谢 p>

解决方案

基于 binascii_unhexlify() 功能:

  #include< cctype> //是* 

int to_int(int c){
if(not isxdigit(c))return -1; //错误:找到非十六进制数字
if(isdigit(c))return c - '0';
if(isupper(c))c = tolower(c);
返回c - 'a'+ 10;
}

模板< class InputIterator,class OutputIterator> int
unhexlify(InputIterator first,InputIterator last,OutputIterator ascii){
while(first!= last){
int top = to_int(* first ++);
int bot = to_int(* first ++);
if(top == -1 or bot == -1)
return -1; //错误
* ascii ++ =(top <= 4)+ bot;
}
返回0;
}



Example



  #include< iostream> 

int main(){
char hex [] =7B5a7D;
size_t len = sizeof(hex) - 1; // strlen
char ascii [len / 2 + 1];
ascii [len / 2] ='\0';

if(unhexlify(hex,hex + len,ascii)<0)return 1; //错误
std :: cout<<十六进制<< - ><< ascii<<的std :: ENDL;



输出



  7B5a7D  - > {z} 

源代码中的评论有趣引用:


当我读数十个编程或解码
格式的程序(documentation?hihi :-)时,我制定了Jansen的
观察:


以ASCII格式编码二进制数据的程序以
这样的风格编写,它们尽可能不可读。使用的设备包括
不必要的全局变量,将重要的表埋入不相关的
源文件中,将函数放入包含文件中,使用
表达式描述变量名称用于不同目的,调用
空子例程和其他人。

我试图打破这个传统,但我猜想
确实使性能不是最优的。哦,太糟糕了......

CWI的Jack Jansen,1995年7月。


I want to build a function to easily convert a string containing hex code (eg. "0ae34e") into a string containing the equivalent ascii values and vice versa. Do I have to cut the Hex string in pairs of 2 values and gue them together again or is there a convenient way to do that?

thanks

解决方案

Based on binascii_unhexlify() function from Python:

#include <cctype> // is*

int to_int(int c) {
  if (not isxdigit(c)) return -1; // error: non-hexadecimal digit found
  if (isdigit(c)) return c - '0';
  if (isupper(c)) c = tolower(c);
  return c - 'a' + 10;
}

template<class InputIterator, class OutputIterator> int
unhexlify(InputIterator first, InputIterator last, OutputIterator ascii) {
  while (first != last) {
    int top = to_int(*first++);
    int bot = to_int(*first++);
    if (top == -1 or bot == -1)
      return -1; // error
    *ascii++ = (top << 4) + bot;
  }
  return 0;
}

Example

#include <iostream>

int main() {
  char hex[] = "7B5a7D";
  size_t len = sizeof(hex) - 1; // strlen
  char ascii[len/2+1];
  ascii[len/2] = '\0';

  if (unhexlify(hex, hex+len, ascii) < 0) return 1; // error
  std::cout << hex << " -> " << ascii << std::endl;
}

Output

7B5a7D -> {Z}

An interesting quote from the comments in the source code:

While I was reading dozens of programs that encode or decode the formats here (documentation? hihi:-) I have formulated Jansen's Observation:

Programs that encode binary data in ASCII are written in such a style that they are as unreadable as possible. Devices used include unnecessary global variables, burying important tables in unrelated sourcefiles, putting functions in include files, using seemingly-descriptive variable names for different purposes, calls to empty subroutines and a host of others.

I have attempted to break with this tradition, but I guess that that does make the performance sub-optimal. Oh well, too bad...

Jack Jansen, CWI, July 1995.

这篇关于处理从和到十六进制的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆