ich verwende ein char array, in dem Hex-werte gespeichert sind. Ich muss die Hex-Werte später als char auf die serielle Schnittstelle schieben und diese werden von mehreren Atmega8-µcs gelesen. Das funktioniert auch prima mit einem befüllten Array und einer Schleife:
char mot[5] = {0xF5, 0x1E, 0x3C, 0x00, 0x00};
for (int i = 0: i < 5; i++) {
Serial.print(mot*);* } * jetzt möchte ich aber die HEX-Werte dynamisch verändern und im Array speichern. Ich nehme dazu Dezimalwerte (0-200) her die sich aus verschiedenen Schritten errechnen, die ich in Hex umwandlen muss also z.B. 30 Dez --> 1E HEX. Die Umwandlung von Dez. in Hex. soll ist nicht das primäre Problem sein, das kann ich am Papier natürlich. Es geht mehr darum wie ich das Ergebnis der Umwandlung in einen char schreibe. * jetzt muss aber vor das 1E noch das 0x --> 0x1E und dieser Wert muss als char ins Array auf den richtigen Platz. Wie füge ich den Präfix vor? Gibt es in C geeignete Befehle damit der Wert 0x1E als char rauskommt? Ich hoffe das war einigermaßen verständlich und ihr könnt mir weiterhelfen. Danke und Gruß Robert
I use a char array, that stores 5 Hex-bytes. The bytes have to be written to the seial port, where a couple of atmega8-microcontrollers read them. It works properly with a char array and a loop:
char mot[5] = {0xF5, 0x1E, 0x3C, 0x00, 0x00};
for (int i = 0: i < 5; i++) {
Serial.print(mot*);* } *So far so good, but now I want to replace the hex-values of the array dynamically. The hex-values are calculated from decimal values (0-200). The conversion from dec to hex should not be the problem, the problem ist to store the outcome of the conversion (i.e. 30 --> 1E) in a char with format 0x1. * I need to write char values to the serial port. Integers don't work unfortunately. The physical serial output looks different when sending integer values instead of chars as I saw on the oscilloscope. * Can anyone give me a hint or tell me c-methods for conversion? Thanks!
*Robert *
Data is stored in memory in binary format. Whether you initialize mot[0] with 0xF5 or 245 or B11111001 makes no difference. The exact same sequence of bits is set (if I did the math right).
Integers don't work unfortunately. The physical serial output looks different when sending integer values instead of chars as I saw on the oscilloscope.
That's because integers and chars are different sizes.
Bytes and chars ARE the same size and should result in the same bit pattern being sent.