Go Down

### Topic: Question about String (Read 703 times)previous topic - next topic

#### tensa

##### Feb 02, 2013, 02:47 am
good day sir,

if im going to send a string consisting 4 bytes of character using the serial monitor, is it possible for me to store the whole string as a single HEX value?

thanks

#### GoForSmoke

#1
##### Feb 02, 2013, 02:52 am
A hex value uses 4 bits, 0-15 = 0x0 - 0xF

ASCII text characters are 8-bit values, 0-255 = 0x00 - 0xFF
4 of those takes 32 bits.

Are you asking to store 32 bits of data in 4 bits of hex?

Nick Gammon on multitasking Arduinos:
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### tensa

#2
##### Feb 02, 2013, 02:57 am

A hex value uses 4 bits, 0-15 = 0x0 - 0xF

ASCII text characters are 8-bit values, 0-255 = 0x00 - 0xFF
4 of those takes 32 bits.

Are you asking to store 32 bits of data in 4 bits of hex?

no sir, i mean, i just want those 32bits of data to have its corresponding hex value, i have read about the checksum to do this but i dont understand it completely

#### PeterH

#3
##### Feb 02, 2013, 03:09 am
I don't see the connection with checksums. Are you saying you want to declare a sequence of four ascii characters in terms of hex values rather than ascii values? You can do that just by declaring an initialised char array with the element values defined as hex literals, although it seems like a strange thing to do. If you're going to treat it like a string, remember to include a terminating byte in your character array.

#### tensa

#4
##### Feb 02, 2013, 03:21 am
for example sir, the RFID tag example, it will send 12bytes of ascii characters to the arduino via rfid reader, then each byte will be read and compared to the next using check sum(if i understand it right from the sample code), then the process continues until all 12bytes are complete, and after that he already have a HEX value for the RFID tag(this part is the one that seems foggy to me).

So im curious if there are any other ways of doing this

#### PeterH

#5
##### Feb 02, 2013, 03:56 am
It may be polite to call strangers 'sir' in your culture, but in my culture it's not appropriate - please stop it.

It sounds as if you want to compose a message which consists of a combination of ascii and binary data. In general that's not the sort of thing that you would usually hard-code (instead, you would compose the message from data) but you can define it using initialised data if that's what you want to do.

Code: [Select]
`char message[] = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', 0xFF, 0xFA, 0xFB, 0xFD};`

Note that this is a character array but it isn't an ascii string (there are non-printable characters in it and it is not terminated) so many of the functions available to deal with normal ascii strings wouldn't work. But if you have a particular need to construct a hard-coded message combining ascii and hex characters, that's one way to do it.

#### GoForSmoke

#6
##### Feb 02, 2013, 03:59 am
Fpr modular checksum.

You use unsigned 8-bit ints (bytes) and just add the value of each new byte to the last. Don't worry about overflow. The modular checksum is that final byte inverted and add 1 (2's complement). With that anyone can check the data.

Nick Gammon on multitasking Arduinos: