如何将一个数字(大于 8 个字节)从 char 数组转换为其 ASCII 表示形式?

How to convert a number (bigger than 8 bytes) from a char array to its ASCII representation?

我在 Windows-XP 平台上,使用 C++98。这是我的问题:

  1. 我收到一个以 base64 编码的字符数组中的数字。数字是 16 字节长
  2. 将数字解码为 base10 后,我得到了一个 12 字节长的数字
  3. 应该将这 12 个字节(仍在 char 数组中)转换为 ASCII 表示形式(要求)。

一个例子:

我目前卡在了最后一步,我不知道该怎么做。 当我搜索时,我唯一发现的是如何将 int 转换为字符串(因此,对于包含值 10 的 int,我猜 sprintf("%d", buffer) 可以做到这一点。因为我的数字更大超过 8 bytes/64 位(12 字节)并且没有存储在 int/unsigned long long 中,我很确定我需要做其他事情来转换我的号码。

如有任何帮助,我们将不胜感激

编辑代码示例(link 到 base64 库:http://libb64.sourceforge.net/

#include <iostream>
#include <cstring>
#include "b64/decode.h"

class Tokenizer{
public:
    Tokenizer();
    void decodeFrame(char *buffer, int bufferSize);
    void retrieveFrame(char *buffer);
private:
    char m_frame[255];
};

using namespace std;
int main(int argc, char *argv[])
{
    Tokenizer token;

    char base64Message[] = "J+QbMuDxYkxxtSRA";  // base64 message of 16 (revelant) bytes
    char asciiMessage[30] = {0};                // final message, a number in ascii representation of 29 bytes

    cout << "begin, message = " << base64Message << endl;

    token.decodeFrame(base64Message, 16);              // decode from base64 and convert to ASCII
    token.retrieveFrame(asciiMessage);          // retrieve the ASCII message

    cout << "final message : " << asciiMessage << endl;                       // the message should be "12345678912345678912345678912"

    return 0;
}
Tokenizer::Tokenizer()
{
}
void Tokenizer::decodeFrame(char *buffer, int bufferSize)
{
    memset(m_frame, 0x00, 255);
    char decodedFrame[255] = {0};  // there is maximum 255 byte in ENCODED (base64). When decoded, the size will automatically be lower
    int decodedSize = 0;
    base64::decoder base64Decoder;  // base64 to base10 decoder, found there: http://libb64.sourceforge.net/

    decodedSize = base64Decoder.decode(buffer, bufferSize, decodedFrame); // int decode(const char* code_in, const int length_in, char* plaintext_out)

    // the frame now must be decoded to produce ASCII, I have no idea how to do it
    for(int i = 0; i < 30; i++){    m_frame[i] = decodedFrame[i]; }
}
void Tokenizer::retrieveFrame(char *buffer)
{
    if(buffer != NULL){
        for(int i = 0; m_frame[i] != 0; i++){
            buffer[i] = m_frame[i];
        }
    }
}

@Farouk:您的推理错误是由于将 base64 字符串直接转换为十进制 (base10)。实际上,此转换会更改原始数字,您在转换中跳了一步以获取更多详细信息,请参见以下内容: 例子 : 接收 base64 : J+QbMuDxYkxxtSRA

**your steps :**
**base64 -> dec**  : 39 18162 3170 76 113 22784
**dec -> hex**     : 153D8AE2CE845159A0

**logical steps:**
**base64 -> hex**  : 27e41b32e0f1624c71b52440
**hex -> deci**    : 12345678912345678912345678912
**deci -> hex**    : 27E41B32E0F1624C71B52440
**hex -> base64**  : J+QbMuDxYkxxtSRA

对于转换 base64tohex 的 c++ 解决方案,我认为您可以在网上找到一个库,如果没有,我可以稍后为您编写此转换代码。

earlier answer 的基础上,您可以轻松地从一堆字节构建任意长度的整数。

const char *example = "\x27\xE4\x1B\x32\xE0\xF1\x62\x4C\x71\xB5\x24\x40";
Bignum bn = 0;
for (int i = 0; i < 12; ++i)
{
    bn *= 256;
    bn += example[i] & 0xff;
}
std::cout << bn << std::endl;

在此处查看完整代码:http://coliru.stacked-crooked.com/a/d1d9f39a6d575686