本文介绍了从C ++ hex2bin中缺少标点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试复制PHP的 bin2hex($ s) pack('H *',$ s) (在PHP 5.4.3+中的aka hex2bin($ s))在GCC / Linux C ++,我似乎有它的想法,除了它的一些奇怪的原因丢弃标点符号。你能弄清楚我在hex2bin()函数中可能做错了什么?我比较PHP的bin2hex()与我的,它似乎是在那里正常工作,所以问题是在hex2bin()。

While trying to duplicate PHP's bin2hex($s) and pack('H*',$s) (aka hex2bin($s) in PHP 5.4.3+) in GCC/Linux C++, I seem to have it figured out except that it's dropping punctuation for some strange reason. Can you figure out what I might be doing wrong in the hex2bin() function? I compared PHP's bin2hex() with mine and it appears to be working there properly, so the problem is in hex2bin().

#include <strings.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

string bin2hex(string s) {
  int nLen = s.length();
  string sOut;
  char cBuff[2];
  for (int i = 0; i < nLen; i++) {
    sprintf(cBuff,"%.2x",s[i]);
    sOut.append(cBuff);
    cBuff[0] = '\0';
  }
  return sOut;
}

string hex2bin(string s) {
  int nLen = s.length();
  string sOut;
  char cBuff1[2];
  char cBuff2[2];
  char cBuff[1];
  int n,n1,n2;
  for (int i = 0; i <= nLen; i+=2) {
    sprintf(cBuff1,"%c",s[i]);
    sprintf(cBuff2,"%c",s[i+1]);
    n1 = atoi(cBuff1);
    n2 = atoi(cBuff2);
    n = (n1 * 16) + n2;
    sprintf(cBuff,"%c",n);
    sOut.append(cBuff);
    cBuff[0] = '\0';
    cBuff1[0] = '\0';
    cBuff2[0] = '\0';
  }
  return sOut;
}

int main() {
  string s;
  string sResult;  
  s = "This is a 123 test.";
  sResult = bin2hex(s);
  printf("ENCODED: %s\n",sResult.c_str());
  sResult = hex2bin(sResult);
  printf("UNENCODED: %s\n",sResult.c_str());
  return 1;
}

这会产生:

ENCODED: 5468697320697320612031323320746573742e
UNENCODED: This is a 123 test


推荐答案

好吧,袖子卷起来:让我们看看C ++版本:

Okay, sleeves rolled up: let's look at C++ version:

Live on Coliru


  • 不要使用C字符串,除非你需要(sprintf构建一个两字符串不是...非常有效)

  • 使用iostreams编码/数字( std :: hex

  • 可以优化 hex2bin hex2bin
  • 添加了
  • Don't use C strings unless you need to (sprintf to build a two-char string is not... very efficient)
  • Use iostreams to encode/decode the hex digits (std::hex)
  • The hex2bin could optimized, but I went for "simpler"
  • I added a modicum of input sanitizing on hex2bin
#include <string>
#include <sstream>
#include <iomanip>

std::string bin2hex(std::string const &s) {
    std::ostringstream oss;

    for (auto ch : s)
        oss << std::hex << std::setw(2) << std::setfill('0') << (int) ch;

    return oss.str();
}

#include <cassert>
std::string hex2bin(std::string const& s) {
    assert(s.length() % 2 == 0);

    std::string sOut;
    sOut.reserve(s.length()/2);

    std::string extract;
    for (std::string::const_iterator pos = s.begin(); pos<s.end(); pos += 2)
    {
        extract.assign(pos, pos+2);
        sOut.push_back(std::stoi(extract, nullptr, 16));
    }
    return sOut;
}

#include <iostream>
int main() {
    std::cout << "ENCODED: " << bin2hex("This is a 123 test.")          << "\n";
    std::cout << "DECODED: " << hex2bin(bin2hex("This is a 123 test.")) << "\n";
}

输出:

ENCODED: 5468697320697320612031323320746573742e
DECODED: This is a 123 test.

这篇关于从C ++ hex2bin中缺少标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 21:41