I am on a Windows-XP platform, using C++98. Here is my problem:
- I receive a number encoded in base64 in a char array. The number is 16 bytes long
- After decoding the number to base10, I end up with a number of 12 bytes long
- Those 12 bytes (still in a char array) should be converted to a ASCII representation (requirement).
An example:
- Reception of data : "J+QbMuDxYkxxtSRA" (in a char array : 16 bytes)
- Decoding from base64: 0x27 0xE4 0x1B 0x32 0xE0 0xF1 0x62 0x4C 0x71 0xB5 0x24 0x40 (hex in a char array: 12 bytes)
- Conversion from decimal to ASCII : "12345678912345678912345678912" (in a char array: 29 bytes)
I am currently stuck at the last step, I have no idea how to do it. When I searched, the only thing I found is how to convert from int to string (so, for an int containing the value 10, a sprintf("%d", buffer) would do the trick I guess. Since my number is greater than 8 bytes/64 bits (12 bytes) and is not stored in a int/unsigned long long, I am pretty sure I need to do something else to convert my number.
Any help would be greatly appreciated
EDIT for a code example (link to the base64 library : 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];
}
}
}