如何将一个数字(大于8个字节)从字符阵列转换为其ASCII表示

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

本文关键字:转换 阵列 字符 表示 ASCII 字节 大于 一个 数字 8个      更新时间:2023-10-16

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

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

一个例子:

  • 数据接收:" J qbmudxykxxtsra"(在char数组:16字节中)
  • 从base 64:0x27 0xe4 0x1b 0x32 0xe0 0xf1 0x62 0x62 0x4c 0x4c 0xb5 0x24 0x24 0x40(char阵列中的十六进制:12 bytes)
  • 从十进制到ASCII的转换:" 12345678912345678912345678912"(在char阵列:29个字节中)

我目前被困在最后一步,我不知道该怎么做。当我搜索时,唯一发现的是如何从int转换为字符串(因此,对于包含值10的int,sprintf("%d",buffer)会做到我猜的技巧。因为我的数字更大超过8个字节/64位(12个字节),并且不存储在int/inted long中,我很确定我需要做其他事情来转换我的电话号码。

任何帮助将不胜感激

编辑代码示例(链接到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];
        }
    }
}

在较早的答案上构建您可以轻松地从一堆字节构建任意长度的整数。

const char *example = "x27xE4x1Bx32xE0xF1x62x4Cx71xB5x24x40";
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

@farouk:您的理由错误是直接在转换base64字符串到十进制(base10)的。确实,此转换更改了原始数字,您已经在转换中跳了一步,以获取更多详细信息,请参见以下: 例子 : 接收基本64: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

对于解决方案C 转换base64tohex,我认为您可以在网上找到库,如果不是,我可以为您编码此转换。