Hi,
I wonder if someone has a piece of code which convert my unsigned char array with binaries to long or int.
The array is fix with 24 elements.
Would be happy if someone can help me out
Hi,
I wonder if someone has a piece of code which convert my unsigned char array with binaries to long or int.
The array is fix with 24 elements.
Would be happy if someone can help me out
const unsigned int ArraySize = 24;
unsigned char input[ArraySize];
unsigned long Output[ArraySize];
void setup(){
  for (int i=0; i< ArraySize; i++)
    Output[i] = Input[i];
}
void loop() {}
I realize that a silent cast (i.e., assigning a smaller data type into a larger data type) doesn't require an explicit cast, but I still prefer to document them:
    Output[i] = (unsigned long) Input[i];
Thank you all. I realize that my question was wrong.
My array looks like this:
bit[0] = 1
bit[1] = 1
bit[2] = 0
bit[3] = 0
bit[4] = 1
...
bit[23] = 1
so, when I put all the binaries in a line, I have many 1 and 0 which I want to convert to decimal integer or long.
unsigned long convertBitArray(byte Input[], int length) {
 if(length<=0 || length > 32) return; //don't attempt to process values outside the available length of unsigned long
 unsigned long result = 0;
 for(int i=0; i<length; i++) {
  result = result<<1 + Input[i]?0:1; //interprets the byte value to be 0 or 1
 }
 return result;
}
With your function I get the 1 and 0 again.
All I need is the decimal value. For example 101101 = 45 in decimal.
Post your code. The function given to you creates a number. How you choose to display that number in human readable form is up to you. All numbers are binary within the Arduino. Only on display are they converted to another format (decimal, hexadecimal, octal).
Sorry for all the mistakes. I hope this example code give more clearness
void setup() {
unsigned char databits[10]; // stores all of the data bits}
void loop() {
// binary 1001010101 = 597 in decimal
databits[0] = 1;
databits[1] = 0;
databits[2] = 0;
databits[3] = 1;
databits[4] = 0;
databits[5] = 1;
databits[6] = 0;
databits[7] = 1;
databits[8] = 0;
databits[9] = 1;//how to convert?
}
You are using the type "unsigned char". For consistency of Arduino programming style, the "byte" data type is to be preferred.
It's still not clear to me if your data is of the form where a 0 and 1 are actual numbers, or if they are the char '0' and char '1' with ascii values 48 and 49.
Assuming that you have actual numbers here is some conversion code in the format you presented
byte dataBits[10];
void setup() {
 Serial.begin(115200);
Â
 dataBits[0] = 1;
 dataBits[1] = 0;
 dataBits[2] = 0;
 dataBits[3] = 1;
 dataBits[4] = 0;
 dataBits[5] = 1;
 dataBits[6] = 0;
 dataBits[7] = 1;
 dataBits[8] = 0;
 dataBits[9] = 1;
 Serial.println(convertBitArray(dataBits, 10), BIN);
 Serial.println(convertBitArray(dataBits, 10), DEC);
}
void loop(){}
unsigned long convertBitArray(byte Input[], int length) {
 if (length <= 0 || length > 32)
  return 0; //don't attempt to process values outside the available length of unsigned long
 unsigned long result = 0;
 for (int i = 0; i < length; i++) {
  result = (result << 1) + Input[i];
 }
 return result;
}
Hello cattledog,
thank you very much!
If I understand it right, the print / println method have an optional Pararameter which I can set the result as binary, hex or decimal. This works very well.
But, is it possible to assign a long variable as the decimal result?
Edit: Found it!
For everybody who have the same problems:
You can assign a variable with parameter. This seems to be very special in arduino:
long cardId = convertBitArray(dataBits, bitCount), DEC;
Edit: Found it!
For everybody who have the same problems:
You can assign a variable with parameter. This seems to be very special in arduino:
long cardId = convertBitArray(dataBits, bitCount), DEC;
Can you please provide a small test sketch which demonstrates your problem, or the difference between
long cardId = convertBitArray(dataBits, bitCount), DEC;
long cardId = convertBitArray(dataBits, bitCount);
What you say makes no sense to me. All data lives in computer memory as binary. The default output for Serial.print(var) is in decimal, so If you don't add the specifier, the print will be in decimal.
,DEC does nothing. You could write ,true or ,42 and it would still work the same.
But in a Serial.print() then it makes a big difference. But DEC is the default, so it has the same result as not putting it in.