Recent

Author Topic: How do you convert a char to a byte?  (Read 21988 times)

Pasqualish

  • Jr. Member
  • **
  • Posts: 73
How do you convert a char to a byte?
« on: June 09, 2016, 09:18:16 am »
I want to do Shr on a char. But it doesn't allow that. I can do it on a byte, however I can't directly assign a char to a byte. Is there syntax to cast a char to a byte?

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How do you convert a char to a byte?
« Reply #1 on: June 09, 2016, 09:24:38 am »
I'm a bit surprised with that question as you already did in your previously question in which you shared some of your code. But perhaps i misunderstood your question here ?

Code: [Select]
var
  x : Char;
  y : Byte;
begin
  x := 'a';
  y := Ord(x);
  writeln(y);
  y := Byte(x);
  writeln(y); 
  y := ord(x) shr 1;
end.

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #2 on: June 09, 2016, 09:28:36 am »
Code: Pascal  [Select][+][-]
  1. var a: char;
  2. begin
  3.  
  4.   a:='x';
  5.   { the following line is what you are looking for }
  6.   a:= chr(byte(a) shr 1);
  7. end;  
  8.  

the transtyping of variable a from type char to byte is : "byte(a)"
cast done.
« Last Edit: June 09, 2016, 09:43:56 am by sam707 »

Pasqualish

  • Jr. Member
  • **
  • Posts: 73
Re: How do you convert a char to a byte?
« Reply #3 on: June 09, 2016, 09:41:17 am »
the transtyping of variable a from type char to is : "byte(a)"

Thanks! So I guess - in cases where it is possible - the Pascal "cast" would be in that form for all values such as Integer(a), LongInt(a) etc.

Pasqualish

  • Jr. Member
  • **
  • Posts: 73
Re: How do you convert a char to a byte?
« Reply #4 on: June 09, 2016, 09:42:35 am »
I'm a bit surprised with that question as you already did in your previously question in which you shared some of your code. But perhaps i misunderstood your question here ?

No, you are correct. I  looked at my output erroneously and decided that Ord() had produced the wrong result.

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #5 on: June 09, 2016, 09:45:36 am »
transtyping ... casting from simple type to another is type(value) so, you did guess it right way!

Enjoy

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #6 on: June 09, 2016, 09:48:37 am »
Ord like Chr are system unit functions, unlike casting from type to type, they arn't usually resolved at compile time but called at run time... that is why you should prefer for faster code Char(value) as Chr(value) , Byte(value) as Ord(value)

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How do you convert a char to a byte?
« Reply #7 on: June 09, 2016, 09:51:46 am »
@Pasqualish
Ok, no problem. thank you for having taken the time to clear that up. Another mystery solved  :)

Wrong interpretation of results is something that happens to each and all of us one time or another. Can be very hazardous, although it keeps things exciting  :D

Thaddy

  • Hero Member
  • *****
  • Posts: 19262
  • Glad to be alive.
Re: How do you convert a char to a byte?
« Reply #8 on: June 09, 2016, 11:35:09 am »
Note all examples given are not really portable because in all cases it is assumed that char is AnsiChar, which is not always the case.
So declare it as AnsiChar, plz.
Code: Pascal  [Select][+][-]
  1. program charshift;
  2. {$APPTYPE CONSOLE}
  3. begin
  4.   writeln(AnsiChar(Byte(AnsiChar('z')) shr 1)); // should be '='
  5.   readln;
  6. end.

The AnsiChar casts are not necessary on Ansi systems, but is on systems where the string literal is an unicode encoding and where Char is WideChar.
« Last Edit: June 09, 2016, 11:44:38 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #9 on: June 09, 2016, 12:04:56 pm »
Note all examples given are not really portable because in all cases it is assumed that char is AnsiChar, which is not always the case.
So declare it as AnsiChar, plz.
Code: Pascal  [Select][+][-]
  1. program charshift;
  2. {$APPTYPE CONSOLE}
  3. begin
  4.   writeln(AnsiChar(Byte(AnsiChar('z')) shr 1)); // should be '='
  5.   readln;
  6. end.

The AnsiChar casts are not necessary on Ansi systems, but is on systems where the string literal is an unicode encoding and where Char is WideChar.

we could discuss a whole book long on what is portable or not because ansichar did NOT exist when Turbo pascal normalized what a byte or a char was, and even the "type(value) to cast from type to type is NOT part of the Pascal language 1st syntax form, that is

var a: char;
var b: byte;

begin

...
b := a as byte;

end;

SO!!! as it works and it is understandable, the conversation around AnsiChar, WideChar, UTF8Char, WHATEVERChar, and I8n internationalization would be apart, and on another chapter of a neverending book that I wont troll with anyone...

USELESS!! and cumbersome, at this point
« Last Edit: June 09, 2016, 12:08:08 pm by sam707 »

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #10 on: June 09, 2016, 12:06:22 pm »
what is portable a day, can be no more portable 10 years later and et cetera  ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 19262
  • Glad to be alive.
Re: How do you convert a char to a byte?
« Reply #11 on: June 09, 2016, 01:40:31 pm »
what is portable a day, can be no more portable 10 years later and et cetera  ;)

Yes, but in this case it is all the more true: char in the sense of the olden days of TurboPascal should be considered deprecated. So in the modern times you should use AnsiChar, not Char.
Just because Char is not guaranteed to be one byte anymore. So, yes, your last statement is true and you should use AnsiChar in modern code. That will make your sourcecode a lot more portable between systems for a long time to come. Just define {$delphiunicode} or {$modeswitch unicodestrings} and compile and run your char examples.... Then do the same with proper AnsiChar declarations, then it works again as expected. Keep using char if you want to introduce hard to find bugs in your sourcecode. By all means ;)
« Last Edit: June 09, 2016, 01:53:58 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #12 on: June 09, 2016, 04:05:02 pm »
dear @Thaddy, the original post was an easy question, answered with 1 line of code and, as I just told you, you are at the begining of a whole book around a single byte (?!?!?! LOL), furthermore bluring the original subject and going ahead with useless considerations regarding the original very 1st question... could you, please, keep concentrated on one point only? it seems not. Nevermind, but I seriously think you are the kind of person who always feel the need to get the last word, even if you make people waste their time and get lost around DIFFERENT subjects
« Last Edit: June 09, 2016, 04:19:14 pm by sam707 »

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #13 on: June 09, 2016, 04:07:56 pm »
when I read a post with answers that ALREADY gave the response, I might be lazy, but I do not write'n'troll ;)
BYE
Regards

sam707

  • Guest
Re: How do you convert a char to a byte?
« Reply #14 on: June 09, 2016, 04:11:17 pm »
nothing personal, i saw many many many people here, extending comments and comments, while its unnecessary... so just did I, but its rare from me :D I beg your pardon

 

TinyPortal © 2005-2018