invalid conversion from 'byte* {aka unsigned char*}' to 'char*'
The error is that myBytes need to be a char*, how can I achieve this?
Thanks
This is my code:
#include <SPI.h>
#include <SD.h>
#include <Base64.h>
File myFile;
byte myBytes[200];
int i;
void setup()
{
Serial.begin(9600);
//open the file for reading:
myFile = SD.open("pic.jpg");
if (myFile)
{
for (int aByte = 0; aByte < 200; aByte++)
{
myBytes[aByte] = myFile.read(); //put the byte read from the file into the array
}
//at this point there is 200 bytes of data in the myBytes array
myFile.close();
//base64 encode myBytes
int inputLen = sizeof(myBytes);
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, myBytes, inputLen);
Serial.println(encoded);
}
}
}
void loop()
{
}
Here is code that works.
#include <Base64.h>
/*
Base64 Encode/Decode example
Encodes the text "Hello world" to "SGVsbG8gd29ybGQA" and decodes "Zm9vYmFy" to "foobar"
Created 29 April 2015
by Nathan Friedly - http://nfriedly.com/
This example code is in the public domain.
*/
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Base64 example");
// encoding
char input[] = "Hello world";
int inputLen = sizeof(input);
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
Serial.print(input); Serial.print(" = ");
// note input is consumed in this step: it will be empty afterwards
base64_encode(encoded, input, inputLen);
Serial.println(encoded);
}
void loop()
{
}
What I'm trying to do is read the first 200 bytes of a jpeg file and do base64 encode the information using the base64 library. In order to read the bytes out out of the file, I've declared myBytes[] as a byte type.
However, in the base64 example, hello world is declared as a char as in the base64_encode() function.
I can’t just change myBytes to type char, can I? because I want it to store bytes.
The reason why I was doing that was because here it says:
When you have some binary data that you want to ship across a network, you generally don't do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know -- some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).
So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings. Why 64? Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.
In other words, base64 encoding is used to convert binary data to a text form so that it can pass through places that cannot (for one reason or another) accept binary data.
Some technical details:
base64 uses 4 bytes (or 4 char) of text to transport 3 bytes (or 3 char) of binary data. There is a loss of bandwidth in exchange for transparency.
base64 decoding reverses the process, taking base64 text data and converting it back into binary data.
There are various "flavors" of base64 encoding and decoding. Mostly, the variants differ in the filler used when the quantity of input binary data is not an integer multiple of 3 bytes.