Hello,
I have a long chain of HEX chars. I want to convert them into binary and display this sequence of 0 and 1 to a single LED.
I don't really know how to preceed because i cant declare a hex int longer than 8 chars. Its a pretty long chain so i can't manually intersect it every 8 chars.
What i already have:
int sequence = 0xADA19D52;
int ssize = sizeof(sequence);
void BinaryBlink() {
for(int i=0; i<ssize; i++){
if (bitRead(sequence, i))
{digitalWrite(LedOut,HIGH);}
else{digitalWrite(LedOut,LOW);}
delayMicroseconds(500);
}
}
How can I declare and read out a longer HEX sequence?
system
January 3, 2012, 12:18pm
2
You can't store eight hex digits in an Arduino int.
Try an array of ints.
Btw, sizeof returns the size of the given object in bytes.
system
January 3, 2012, 12:30pm
3
You can't store eight hex digits in an Arduino int.
Agreed.
Try an array of ints
For? Please elaborate. I would have suggested a larger type, such as long, since the value specified will fit in a long.
int sequence = 0xADA19D52;
This value won't fit in an int. It will fit in a long.
int ssize = sizeof(sequence);
Do you have any idea what sizeof() does? It returns the number of bytes that the operand uses. ssize will, in this case, always equal 2.
for(int i=0; i<ssize; i++){
So, you are going to execute this loop two times, and deal with the first 2 (of 16, should be 32) bits.
if (bitRead(sequence, i))
{digitalWrite(LedOut,HIGH);}
else{digitalWrite(LedOut,LOW);}
should be
digitalWrite(LedOut, bitRead(sequence, i));
Thanks for the helpful replys.
Do you have any idea what sizeof() does? It returns the number of bytes that the operand uses. ssize will, in this case, always equal 2.
So how can a count the bit length of a variable? Just sizeof() and multiply it with 8?
system
January 3, 2012, 12:42pm
5
Yes, that will do the trick
So far so good
but i my chain of hex chars is about 1000 signs long
if i try it with a float, there coming up these two errors
float sequence = 0xADA19D52AE1D4AA9C5A966A55AB2AB4956B4966B6319AACD4969AA69AD9659A69AB2D59A6B659A5;
error: integer constant is too large for 'long' type
digitalWrite(LedOut, bitRead(sequence, i));
error: invalid operands of types 'float' and 'int' to binary 'operator>>'
and it seems that bitRead() can only handle int
system
January 3, 2012, 1:02pm
7
but i my chain of hex chars is about 1000 signs long
This is ridiculous. Perhaps you need to explain what you are really trying to do. A 1000 digit number makes no sense on an Arduino.
MarkT
January 3, 2012, 1:17pm
8
If you have a long chain of hex chars , then the obvious way to store them is a char array:
char sequence [] = {0xAD, 0xA1, 0x9D, 0x52, .... } ;
If very long then consider using PROGMEM for storing them.
Ok, maybe this sounds a bit more ridiculous:
I would have replaced the led with a piezo speaker and give it a try how it sounds.
What would be the max length which wouldn't be ridiculous?
system
January 3, 2012, 1:24pm
10
What would be the max length which wouldn't be ridiculous?
Tell us where the data is coming from, first. What does the data represent?
MarkT:
If you have a long chain of hex chars , then the obvious way to store them is a char array:
char sequence [] = {0xAD, 0xA1, 0x9D, 0x52, .... } ;
If very long then consider using PROGMEM for storing them.
Is there a way to store it without seperating it with commas?
Tell us where the data is coming from, first. What does the data represent?
It's coming from the BTc Sound Encoder 3.0 Software through which i have compressed a short sound sample.
system
January 3, 2012, 1:35pm
12
urstruktur:
What would be the max length which wouldn't be ridiculous?
As the others already said, PROGMEM size is the theoretical limit.
If you have a binary data block to include, it's simple and correct to do this via a const char array. Display fonts are a good example.
Just write a small script that exports the binary data to the format the compiler expects, including the separating comma. There is no way to do it without comma.
system
January 3, 2012, 2:33pm
13
@OP , the commas don't consume either RAM or program memory.