Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Convert HEX to CHAR?  (Read 339 times)

• Full Member
• Posts: 121
Convert HEX to CHAR?
« on: September 21, 2022, 06:40:30 pm »
Hallo,
how can I convert a HEX into a Char without special Freepascal Unit? (I use Mikroe Pascal für Atmel AVR)
I have 53 and need S on LCD
Code: [Select]
`program Uart_Test;{ Declarations section }// Lcd module connectionsvar LCD_RS : sbit at PORTC2_bit;var LCD_EN : sbit at PORTC3_bit;var LCD_D4 : sbit at PORTC4_bit;var LCD_D5 : sbit at PORTC5_bit;var LCD_D6 : sbit at PORTC6_bit;var LCD_D7 : sbit at PORTC7_bit;var LCD_RS_Direction : sbit at DDC2_bit;var LCD_EN_Direction : sbit at DDC3_bit;var LCD_D4_Direction : sbit at DDC4_bit;var LCD_D5_Direction : sbit at DDC5_bit;var LCD_D6_Direction : sbit at DDC6_bit;var LCD_D7_Direction : sbit at DDC7_bit;// End Lcd module connections  var beeper : sbit at PortC1_bit; //Piepser  VAR str_length, empfang : WORD;  laenge : WORD;  //Output_str : String[255];      Output_str : array [0..255] of char;  lt, n, read_byte : BYTE;  delim: array [0..3] of char;  output_byte : array[0..255] of char;  //output_byte  : BYTE;  //output_hex  : array[0..42] of CHAR;    procedure Read(); iv IVT_ADDR_USART0__RX; ics ICS_AUTO;  Begin    output_byte[lt] := Uart1_Read();    inc(lt);  end;          begin  { Main program }    DDRC.1:=1;  Lcd_Init();  LCD_CMD(_LCD_CURSOR_OFF);  UART1_Init(9600);   SREG_I_bit := 1;               // Interrupt enable   RXCIE0_bit  := 1;// delim[0] := 0x0A; //delimiter set to LF = 10 = 0x0A//delim[1] := 0x00; //delimiter must end with NULL character  Beeper:=1;  LCD_CMD(_LCD_CLEAR);         LCD_Out(1, 1, 'Start');         delay_ms(1000);lt:=0;     //  for n:=0 to 10 DO UART1_Read();      // RXCIE0_bit  := 1;  While TRUE DO  Begin //IF lt >= 10 THEN BEGIN             ByteToHex(output_byte[34], output_str) ;   //bytetostr(output_byte[34],output_str);    LCD_Out(2,1, Output_str);          delay_ms(100);              lt:=0;       LCD_CMD(_LCD_CLEAR);        RXCIE0_bit  := 1;      //end;        end;end.`
« Last Edit: September 21, 2022, 06:45:39 pm by coradi »
Wind XP / 7 / 10

KodeZwerg

• Hero Member
• Posts: 566
Re: Convert HEX to CHAR?
« Reply #1 on: September 21, 2022, 06:46:59 pm »
Hallo,
how can I convert a HEX into a Char without special Freepascal Unit? (I use Mikroe Pascal für Atmel AVR)
I have 53 and need S on LCD
Code: Pascal  [Select][+][-]
1. WriteLn(Char(\$53));
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

• Full Member
• Posts: 121
Re: Convert HEX to CHAR?
« Reply #2 on: September 21, 2022, 06:50:34 pm »
yeah:-) I think that will be the solution. :-)
But I don't have any Idea to use this, I wll try a lttle
Wind XP / 7 / 10

• Full Member
• Posts: 121
Re: Convert HEX to CHAR?
« Reply #3 on: September 21, 2022, 06:56:45 pm »
ahhh:-)
Not the soluteion, but you helped me already:-)
Code: [Select]
` Output_str[0] :=  CHR(output_byte[34]);`
Wind XP / 7 / 10

Arioch

• Sr. Member
• Posts: 414
Re: Convert HEX to CHAR?
« Reply #4 on: September 21, 2022, 07:02:28 pm »
But what does Chr return, AnsiChar or WideChar ? :-)

Frankly, i long ago stopepd using Chr intrinsic for the sake of explicit typecast

Code: Pascal  [Select][+][-]
1. var
2.   a: byte; b: word;
3.   c: AnsiChar; d: WideChar;
4. ...
5.   c := AnsiChar(a);  Byte(c) := a;
6.   d := WideChar(b); word(d) := b;
7.

This saved my rear parts several times :-)

I understand that ATM having UTF-16 Char as default mode on AVR is unlikely.
But the code tends to by copy-pasted to/from other platforms.

One day that code might end runing on some ARM-M controller and be UTF-16 by default.

I believe for "close to metal" development Char/PChar/string aliases should be no more considered reliable, and replaced with real, "fixed in bits" data types

KodeZwerg

• Hero Member
• Posts: 566
Re: Convert HEX to CHAR?
« Reply #5 on: September 21, 2022, 07:05:10 pm »
Not the soluteion, but you helped me already:-)
You are welcome, my "solution" works for any kind of char (unicode) while chr() is pure ascii (compatible to ansi char)
If you plan to "print" it somehow, i'd suggest to filter out unwanted input-stuff (> 31 up to < 128) or as hex (> 20 up to < 80)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

• Hero Member
• Posts: 12208
Re: Convert HEX to CHAR?
« Reply #6 on: September 21, 2022, 07:12:26 pm »
No, here char is a C'ísm that often occurs with Pascal programmers and is wrong in the first place: you mean byte in Pascal, not char!
I guess he wants the binary notation of the byte involved so he can decide which bit to toggle.
If you want the binary notation to see the individual bits you can use:
Code: Pascal  [Select][+][-]
1. // full program
2. {\$mode objfpc}{\$ifdef mswindows}{\$apptype console}{\$endif}
3. begin
4.   writeln('%',BinStr(\$53,8));
5.   writeln('%',BinStr(\$53 shl 1,8));
6.   readln;  // not necessary, just for the IDE
7. end.
Then you can examine, toggle or set/unset any bit using boolean logic on the value, not the string, but based on clear representation.

Note that all this is not necessary as long as you know the bit pattern for the byte in the first place. This is only representation.
sysutils contains a meriad of bit manipulation helpers, but the above uses just system, and that was asked.
« Last Edit: September 21, 2022, 07:25:20 pm by Thaddy »
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Arioch

• Sr. Member
• Posts: 414
Re: Convert HEX to CHAR?
« Reply #7 on: September 21, 2022, 07:35:18 pm »
Plausible in general, but in this specific case we have

Code: Pascal  [Select][+][-]
1.   output_byte : array[0..255] of char;
2.   //output_byte  : BYTE;
3.   //output_hex  : array[0..42] of CHAR;

When chars and bytes are mixed together i would rather suspect a person who wrote it knows the difference. C-ism would either kepp all the type chars, or with equally broas brush replace all of them into byte.

• Hero Member
• Posts: 12208
Re: Convert HEX to CHAR?
« Reply #8 on: September 21, 2022, 08:37:07 pm »
It should be array of byte. Same mistake by Pascal programmers trying to translate C code into Pascal.
This is really silly and happens all the time, they read char and think "Oh, it is a char, I know that", but it is a byte in Pascal, not a char.
Mixing the two meanings is not a good idea and usually due to a Pascal programmer that has only a little grasp of C. Period.
And no: you should not mix Pascal char and Pascal byte, because a Pascal char can have a whole different meaning, not only depending on mode.
I hope you where not advertising against my statement.
« Last Edit: September 21, 2022, 08:45:04 pm by Thaddy »
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Arioch

• Sr. Member
• Posts: 414
Re: Convert HEX to CHAR?
« Reply #9 on: September 21, 2022, 08:43:26 pm »
It can be both char and byte, depending upon what meanign of data there is and how the code work with it.

And if we assume a programmer, who replaces all char with byte without understanding - he would have do in evey line.

Here we have mixed byte and char, which hints whoever did conversion knew the difference and judged accodingly

• Hero Member
• Posts: 12208
Re: Convert HEX to CHAR?
« Reply #10 on: September 21, 2022, 08:46:45 pm »
It can be both char and byte, depending upon what meanign of data there is and how the code work with it.
C does not support the concept of a Pascal char, that is the problem. In C a char is a byte.
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Arioch

• Sr. Member
• Posts: 414
Re: Convert HEX to CHAR?
« Reply #11 on: September 21, 2022, 09:00:37 pm »
It can be both char and byte, depending upon what meanign of data there is and how the code work with it.
C does not support the concept of a Pascal char, that is the problem. In C a char is a byte.

I know.

And this makes "mechanical" text replacing of all "char" to "byte" wrong
It equally makes  leaving all "char" as is wrong.

The correct approach, by a sapient programmer, is replacing SOME of them, and leaving others.

...and that is exactly what we see.

• Hero Member
• Posts: 12208
Re: Convert HEX to CHAR?
« Reply #12 on: September 21, 2022, 09:03:14 pm »
Correct. You have to know BOTH languages to know how to translate them.
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

Arioch

• Sr. Member
• Posts: 414
Re: Convert HEX to CHAR?
« Reply #13 on: September 21, 2022, 09:23:00 pm »
and then we are back to square 1

since we see Pascal code mixing BYTE and CHAR, rather than only using one of them everywhere, i suppose the coder who wrote it did know the languages