[solved] Word() conversion example

I did a google search of "Arduino word()" and could not find an example of converting a value or other data type. Would someone please share a very simple example.

Can you give an example of what you want an example of? You want to cast something as a word? word is not often used as a type. It is equivalent to int so mostly people use int instead. Or are you talking about the Microsoft word processing application?

Thank you for your quick reply.

I am programming a Pinguino which is nearly Arduino compatible and documenting functions. The Arduino docs show the customary "word type var = val", but they also list word(x).

It is the word(x) that I am interested in. I looked under "C" language and could not find an example.

jwzumwalt: but they also list word(x).

Can you link to where you see that? Maybe that would help figure out what you are talking about.

https://www.arduino.cc/en/Reference/HomePage look under the "Conversion" heading

word() Convert a value to the word data type or create a word from two bytes.

Syntax word(x) word(h, l)

Parameters x: a value of any type h: the high-order (leftmost) byte of the word l: the low-order (rightmost) byte of the word

Returns word

It's pretty self explanatory isn't it? It's not standard C I don't think.

byte low = 1;
byte high = 1;
int together = word(high, low);

Combines the two bytes into an int. So together now has the value 257.

byte low = 1;
byte high = 2;
int together = word(high, low);

Probably a better example so you can tell which byte goes where if you can't from the names high and low. Now together would be 513.

Pinguino is not Arduino, perhaps the word() is not implemented in the same way. http://www.pinguino.cc/

word : https://www.arduino.cc/en/Reference/WordCast

The macro is defined in 'Arduino.h' : https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/Arduino.h

So what about word(x)? It says x is any type...

int a = 5000;

word z = 0;

z = word(a); // is this valid?

For an Arduino Uno, that is valid. I don't know about the Pinguino.

I would recommend avoiding using goofy/cutesy type names like the Arduino "word" type since in most cases cutesy type names like that are not properly defined. For example, in Arduino "word" is simply a typedef for "unsigned int". So that means that on the AVR it is 16 bits but on the ARM and pic32 it will be 32 bits. That is is not portable and is likely to create problems when moving code across processors. The Arduino documentation even states this: https://www.arduino.cc/en/Reference/Word

ANSI C dealt with this kind of stuff years ago (15+ years ago)and created standard types for fixed width types. It is much better to use the types in stdint.h http://www.cplusplus.com/reference/cstdint/ https://en.wikipedia.org/wiki/C_data_types#limits.h So if you want a 16 bit unsigned in you use uint16_t and for 32 bit you use uint32_t

etc... There are other types for other needs like specific widths or minimum widths or speed/optimization preferences.

Using types from stdint ensures you always get the size you want/need no matter the size of an int. Whereas if you use a locally invented type like the Arduino "word", you might as well have used "unsigned int" since that is what you are getting.

My opinion is that the Arduino "word" type was originally intending to give you a 16 bit field which is an "int" on the AVR. The reason being is that historically, "word" or "WORD" was often used to represent a 16 bit field. (This was being done many years ago since it was the only want to have portable code prior to the ANSI standard types - Today is it completely unnecessary) However, when the ARM support was added to Arduino IDE, the Arduino guys failed to typedef "word" as an "unsigned short" on the ARM to also give you a 16 bit value, so in my view the Arduino "word" type is broken on ARM and because of that should be avoided completely even on AVR. None of this would be an issue if Arduino had defined "word" to be "uint16_t" instead of "unsigned int". I believe that the guys that added the ARM support to arduino didn't know what they were doing because having a typedef of "word" that simply defines it as "unsigned int" which behaves differently across architectures is pretty useless. i.e. you might as well just be using "unsigned int".

So again, In my opinion, the best thing is to avoid these cutesy locally invented typedef names and use the standardized ANSI types from stdint for all your types especially for your fixed width types.

--- bill

Why do you want to create a word? Do you even know what a word is? Do you know whether a word is signed or unsigned? Do you know how many bits are in a word?

Do you have any idea why Microsoft came up with that stupid term that means NOTHING? Do you have any idea why they couldn't stick to standard types?

Do you have any idea why the idiots at Arduino got suckered into including word?

PaulS: Do you have any idea why Microsoft came up with that stupid term that means NOTHING? Do you have any idea why they couldn't stick to standard types?

Do you have any idea why the idiots at Arduino got suckered into including word?

I don't think those questions were intended for me,but here are a few more comments on the subject.

It wasn't just Microsoft that used the type word/WORD it was quite commonly used used MANY years before Microsoft even considered leaving pascal for C.

As I said earlier, my suspicion was that the use of the "word" type goes back to usage from years ago and was meant to be a 16 bit value. i.e. a "word" is two bytes.

Decades ago, there were no standard fixed width C types defined and so you had to do that kind of stuff, and it was quite common to use something like "word" or WORD to mean a 16 bit value.

20 to 30+ years ago, it was quite common to see C code that used things like word/WORD, uword/UWORD, word16/WORD16, uword16/UWORD16 being used for 16 bit values. It was an attempt to get a portable 16 bit type across different processors.

So my suspicion is that the new whipper snapper generation of programmers is to young and naive to know about why these data types were used and so they attempt to continue to propagate their use, even they they are no longer needed, and in the process they implement them incorrectly since they lack the understanding of what they were used for.

I think the original Arduino coding was sloppy. They typedefed "word" as a "unsigned int" to be used as a 16 bit quantity as that is what would need to be on the AVR. The DUE guys came along and just copied it rather than define it correctly for the DUE processor. Lacking the understanding of what it was to be used for, they documented the way it was implemented rather than the way it was supposed to work. Had word been typed as uint16_t or int16_t from the beginning, it would have at least been consistent and usable. As it is is now, the word type is unusable if you want the code work consistently across different processors.

In this day and age I would totally avoid types like this as they are unneeded and always write code using the ANSI standard data types in stdint.

--- bill

I enjoyed your trip down memory lane.

In the late 70's and early 80's a lot of my programming was ASM. C may have borrowed from ASM where it was a defacto standard that WORD was 2 bytes and everything was 8bit so there was really no problem - it was a fact of life. All the players where 8bit (Z80, 6502, 80186 & 80286).

The first "C" I programmed was CPM and I don't remember a WORD but I could be wrong. MS "C" was a stolen copy of Gary Kildalls (Digital Research) CPM-C.

Hmm, kind of reminds me of the lemming principle where folks mindlessly follow the guy in front of them until they fall off the cliff!

The 80186 and the 80286 just like the 8086 and 8088 were 16 bit microprocessors... ;)

Not surprising you didn't see WORD as a data type with the C compiler tools. WORD, word, etc... are not native a native C data type. C has very few data types. Even the ANSI C types included in stdint are not native C data types.

--- bill