Serial to hex

Hi all

New to arduino

Trying to have user input a number sometimes with a decimal point sometimes without via the serial window.

I want to convert this number to hex and store it in an array to then send out over ethernetUdp

I have the network side of things working is it just the user input i cannot get working at all

Thanks

Thanks for input. Will be trying them out.
The udp medsage i am sending is to control a theatre lighting console with midi show control. The system is expecting it in hex format so i think i need to send it on that format correct ?

Thanks

The system i am sending goes to the specified lighting cue when it is given this message

So from what you say i could send the message in whichever format i wanted and what i want should still happen. ?

Apologies if this is similar to banging your head on the wall.
But i really apprecaite the help

Many thanks

Byte myArray2 [ ] = {0x47, 0x4d, 0x41, 0x00, 0x49, 0x53, 0x43, 0x00, 0xlc, 0x00, 0x00, 0x00, 0xf0 0x7f 0x7f 0x02, 0x7f, 0x0a, 0x30, 0x2e, 0x30, 0x30, 0x30, 0x00, 0x31, 0x2e, 0x31, 0xf7}; /code]

No, the format is very important. But what do you think format means? In this context (sending things to a controller) it probably is "After a command byte, 3 bytes indicate the value, unless the command is "P" in which case only one byte is the value". Also - andyou have to read the format specification, it will say if it wants the bytes as single letters (one byte one letter) or as "binary/hex" values. The latter means native binary. So that you can specify the pattern of bits you can use binary ( B00110111 is allowed as a binary constant in Arduino) or hex (0x37) or numeric (57) or even as an ASCII char ('7'). All will send the same binary pattern.

Attached is a tiny picture of the same. The squares are really the "binary", even the string of 0-and-1 are letters to represent the binary value.

To see the distinction between the "binary" and the representation thereof, I have included the "real" representation of the number 57 - that number of circles. The symbol "57" is not a number, it is a representation of a number.

So your code example with myArray2 will send a string of bits. the irst one could be the character 'G' orit could be a commandbyte where the first 2 bits say if it is a on/off/toggle the next bit could mean which way to go ... and so on. Ie the bit pattern decides. I do not know, as I have no documentaion on your controller.

text4696.png

DragonControl:
Trying to have user input a number sometimes with a decimal point sometimes without via the serial window.

I want to convert this number to hex and store it in an array to then send out over ethernetUdp

A: Background Information:
1. I have this number: 139; I can make one of the following declarations to store this number in a memory location of the MCU with the understanding and acceptance that data/number is always saved in bit/binary form.

(1)

byte y = 139;       //user has written data/number in the program source code in human format  
Serial.println(y, BIN);   //Serial Monitor shows: 10001011

(2)

byte y = 0x8B;    //user has written data/number in the program source code in hexadecimal format
Serial.println(y, BIN);   //Serial Monitor shows: 10001011

(3)

byte y = 0b10001011;  ////user has written data in the program source code in bit/binary format
Serial.println(y, BIN);   //Serial Monitor shows: 10001011

(4) The following is the actual storage pattern of the given number/data 139 in the memory location of the MCU.
bytePic.png

2. The morale of the story is:
The data/number is always stored in computer/MCU memory in bit form regardless of the format that the user follows to write data in the source code of the program.

3. The following data: 11000110 is found in an arbitrary memory location. How much is it to a human being? Is it 134 or -58 or 'Common Anode (CA) code for Character C'?

The answer depends on 'what keyword' the unknown user put in the program to store this data in the computer memory.

(1) If the used keyword was 'byte', the number would be: 134.
(2) If the used keyword was 'signed char' or 'int8_t', the number would be -58.
(3) It is a CA-code if the bit pattern was derived manually from a 'Character Vs CA-code Lookup Table'.

B: Case Study
The number 14.53 is received by Arduino from the PC via Serial Port. How to save it in a variable as binary?

1. It is said that the number has already been received and now assume that the received number has also been saved in a variable 'float x;. That is we have:

float x = 14.53;

2. The variable x of Step-1 refers to the following 4 memory locations which contain this bit pattern: 01000001 01101000 01110001 11100001 for the number 14.53. There are 32 bits and they can be written in compact form using hex format like this: 416871E1. (This is just to seen on paper; the actual storage is always in bit/binary form.)
floatMem1.png

3. We can use the following union data structure to save the 32-bit data of Step-2 into 4 members of an array; where, low indexed member of the array contains lower byte of the 32-bit data.

union
{
   float x;
   byte myArray[4];
}data;

data.x = 14.53;
Serial.println(data.myArray[0], HEX);   //shows: E1
..........

4. You may ask the question -- how do we know that 01000001 01101000 01110001 11100001 is the bit pattern that has been stored against the number 14.53? Let us execute the following codes:

Serial.println(*(unsigned long*)&x, BIN); //shows: 1000001011010000111000111100001 (leading 0 missing) 
Serial.println(*(unsigned long*)&x, HEX); //shows: 41687AE1

bytePic.png

floatMem1.png