functions opposite to lo and hi lazarus / 9-Editor ---------------------------------------------------------------------- Danail · 2007-02-25 14:36 I need to convert two bytes into a Word numerical type. The first byte has to be the second of the digit. For example byte with code FE and next byte with code FF --->   FE FF  ----> must be convertet to the digit 65534. Is there a procedure or function that to attach these codes to the lower and the upper byte of a Word type variable. ---------------------------------------------------------------------- felipemdc · 2007-02-25 14:49 A trivial solution is: MyWord := LowerByte + $FF * HigherByte; ---------------------------------------------------------------------- Zomis · 2007-02-25 15:13 Don't you mean MyWord := LowerByte + $100 * HigherByte; ? ---------------------------------------------------------------------- felipemdc · 2007-02-25 16:24 yes =) ---------------------------------------------------------------------- Vincent Snijders · 2007-02-25 19:23 Note that this code might not be endian safe. ---------------------------------------------------------------------- Zomis · 2007-02-25 19:25 And by that you mean...? ---------------------------------------------------------------------- Vincent Snijders · 2007-02-25 19:29 I said might so I didn't have to think about it. ;-) On little endian systems (intel i386, amd64) the first byte is the lo value and the next byte is the high value. On big endian systems (powerpc) it is the other way around. ---------------------------------------------------------------------- felipemdc · 2007-02-25 21:42 I think it wouldn´t be endian safe if you utilized shl, and things like that. Using purely aritmetic functions, it´s endian safe. ---------------------------------------------------------------------- Vincent Snijders · 2007-02-25 21:46 It depends on the actual use. But I think it would be safer to cast a word to an array[0..1] of byte or a record first, next: byte; end;. Then you are sure you are using the first byte and the second byte and not the least significant byte and the most significant byte. ---------------------------------------------------------------------- Danail · 2007-02-26 19:33 Thanks for the attention people! I solved the task on another way - by reversing several times from byte to string and from string to hex and int. If it appear that is endian compatable and can be used in all cases you can modify it and add it in the Lazarus. I suppose that other people will need that function too. Here is how I did it: function TwoCharsToWord(a_char_1: Char; a_char_2: Char):Word; var  a_string: String;  a_byte_1: Byte;  a_byte_2: Byte; begin  a_byte_1 := Ord(a_char_1);  a_byte_2 := Ord(a_char_2);  a_string := '$' + IntToHex(a_byte_1, 2) + IntToHex(a_byte_2, 2);  TwoCharsToWord := StrToInt(a_word);{Easy, a but took me 2 days. } end;  If in the big endian it needs to be reversed ... then reverse the chars - just place the second char first and the first char second during programming, like:  the_word := TwoCharsToWord(MyChar2, MyChar1);  except  the_word := TwoCharsToWord(MyChar1, MyChar2); or if there is a way to be understand if the system is endian then you can modify the source function. ---------------------------------------------------------------------- Zomis · 2007-03-15 20:47 I must say that that code doesn't seem to be very fast. And if I'm not mistaken (please correct me if I'm wrong) the above code returns the same thing as this: Code: [Select]function TwoCharsToWord(a_char_1: Char; a_char_2: Char):Word; begin   Result:=Ord(a_char_1) * $100 + Ord(a_char_2); end; Does anyone have any idea how to make this perfectly endian safe as well? ---------------------------------------------------------------------- Marc · 2007-03-16 10:14 if you want char 1 always the first in memory, then stop using a word. Use something like: Code: [Select]type TTwoChar = array[0..1] of Char; and use it as result. if the result has to be a word then use: Code: [Select]function TwoCharsToWord(a_char_1: Char; a_char_2: Char):Word; var   TwoChar = array[1..2] of Char; begin   TwoChar[1] := a_char_1;   TwoChar[2] := a_char_2;   Result:=Word(TwoChar); end; or shorter: Code: [Select]function TwoCharsToWord(a_char_1: Char; a_char_2: Char):Word; var   TwoChar = array[1..2] of Char; absolute Result; begin   TwoChar[1] := a_char_1;   TwoChar[2] := a_char_2; end; BTW... this code isn't opposite to Hi and Lo (the answer Zomis gave is)