如何将一个数字(大于8个字节)从字符阵列转换为其ASCII表示
How to convert a number (bigger than 8 bytes) from a char array to its ASCII representation?
我在Windows-XP平台上,使用C 98。这是我的问题:
- 我在char阵列中收到一个在base64中编码的数字。该数字是16个字节长
- 将数字解码为base10后,我最终得到了12个字节长
- 这些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,我认为您可以在网上找到库,如果不是,我可以为您编码此转换。
相关文章:
- 没有从阵列<float>到阵列<int>的可行转换
- 从较小的阵列到较大的阵列的元素级转换
- 使用 std::transform 在 2d C 阵列上转换为 1d C 数组
- 我需要将阵列样式的邻接矩阵转换为矢量样式(以使其看起来更好)
- 可以使用工会将字符阵列转换为浮动
- 将字符串阵列中的字符转换为浮点
- 我如何将整数转换为存储在字符阵列中的十六进制字节
- 使用高级构造函数将阵列转换为Armadillo Vec
- 将斑点阵列转换为ints阵列
- MBED RTOS将Float转换为字符串/字符阵列
- 将std ::向量转换为阵列和当时的p/调用它会导致访问违规例外,在编组期间mscorlib.dll
- 将valarray转换为基本阵列
- 如何将一个数字(大于8个字节)从字符阵列转换为其ASCII表示
- 将整数转换为阵列(包括领先的零),而无需使用字符串
- 快速将双重指针阵列转换为单个指针,可能是连续的
- 将INT转换为具有选定格式的char阵列
- 将int阵列转换为c 中int的向量
- 如何将一组十六进制字符串转换为字节阵列
- 如何将浮点转换为长度为4的字节阵列(char*的阵列)
- 最快可能的阵列结构,用于转换阵列