Hello,
I need to make a function that returns a byte array. However I am realizing that this is not as simple as it sounds. Attached is my code. I have a returnbyte() function (which just returns the input) but it returns wrong value! Any ideas on how to fix?
Thank you
void dumpByteArray(const byte * byteArray, const byte arraySize)
{
for (int i = 0; i < arraySize; i++)
{
Serial.print("0x");
if (byteArray[i] < 0x10)
Serial.print("0");
Serial.print(byteArray[i], HEX);
Serial.print(", ");
}
Serial.println();
}
void hexCharacterStringToBytes(byte *byteArray, char *hexString)
{
bool oddLength = strlen(hexString) & 1;
byte currentByte = 0;
byte byteIndex = 0;
for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
{
bool oddCharIndex = charIndex & 1;
if (oddLength)
{
// If the length is odd
if (oddCharIndex)
{
// odd characters go in high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Even characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
else
{
// If the length is even
if (!oddCharIndex)
{
// Odd characters go into the high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Odd characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
byte nibble(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}
byte returnbyte(byte inputbyte)
{
return inputbyte;
}
void setup() {
Serial.begin(115200);
byte bytearray[32];
char* value1 = "30c4710d514a60ba4bc1059a964338a905e9b7401eef1170bbc9f2832a6043bc";
hexCharacterStringToBytes(bytearray, value1);
dumpByteArray(bytearray,32);//returns correct value
dumpByteArray(returnbyte(bytearray),32); //this returns wrong value!
}
void loop(){
}
Actually, there are several methods to achieve the same effect. And there's nothing foolish about it.
The most common way is to actually pass a pointer to the array INTO the function which then modifies or fills it as required. In this case the array must be defined in the calling function (or be global). Unless the array is a c-string (null-terminated char array), you will probably need to also pass its size into the function. The 'sizeof' preprocessor directive is handy for this.
A less common way is to return a pointer to the array FROM the function. The thing you have to watch out for here is if the array is local to the function. In that case, it must be 'static'. Otherwise it will cease to exist once the function exits. The other trouble with this method is that the calling function has no way of knowing the size of the returned array. So, it's most useful for c-strings.
EDIT:
I see the person who called you foolish has deleted their post. Just as well, they didn't know what they were talking about.
Koepel:
When you change something, please show the full sketch.
gfvalvo:
Hard to tell from what you've shown us. Post complete code. If what you have is too big and cluttered, post an MCVE that demonstrates the problem.
I had it in my first post and I just made a few changes to it, so here's the changed code
byte temparray[32];
void dumpByteArray(const byte * byteArray, const byte arraySize)
{
for (int i = 0; i < arraySize; i++)
{
Serial.print("0x");
if (byteArray[i] < 0x10)
Serial.print("0");
Serial.print(byteArray[i], HEX);
Serial.print(", ");
}
Serial.println();
}
void hexCharacterStringToBytes(byte *byteArray, char *hexString)
{
bool oddLength = strlen(hexString) & 1;
byte currentByte = 0;
byte byteIndex = 0;
for (byte charIndex = 0; charIndex < strlen(hexString); charIndex++)
{
bool oddCharIndex = charIndex & 1;
if (oddLength)
{
// If the length is odd
if (oddCharIndex)
{
// odd characters go in high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Even characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
else
{
// If the length is even
if (!oddCharIndex)
{
// Odd characters go into the high nibble
currentByte = nibble(hexString[charIndex]) << 4;
}
else
{
// Odd characters go into low nibble
currentByte |= nibble(hexString[charIndex]);
byteArray[byteIndex++] = currentByte;
currentByte = 0;
}
}
}
}
byte nibble(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0; // Not a valid hexadecimal character
}
void returnbyte(byte* inputbyte)
{
for (int i=0; i < sizeof(inputbyte); i++) {
temparray[i] = inputbyte[i];
}
}
void setup() {
Serial.begin(115200);
byte bytearray[32];
char* value1 = "30c4710d514a60ba4bc1059a964338a905e9b7401eef1170bbc9f2832a6043bc";
hexCharacterStringToBytes(bytearray, value1);
dumpByteArray(bytearray,32);//returns correct value
returnbyte(bytearray);
dumpByteArray(temparray,32); //this returns wrong value!
}
void loop(){
}
AWOL: for (int i=0; i < sizeof(inputbyte);Two iterations. Every time
void returnbyte(byte* inputbyte)
{
for (int i=0; i < sizeof(inputbyte); i++) {
temparray[i] = inputbyte[i];
}
"inputbyte" is a pointer. The size of a pointer is 2 bytes (on an AVR processor, 4 on an ARM). That's why I told you that the size of the array must be passed in.