Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 18, 2003 from char[] to byte | ||||
---|---|---|---|---|
| ||||
Hi, how can i cast from char[] to byte? import outbuffer; int main () { //byte by = 0x4d; byte by; OutBuffer buf = new OutBuffer(); char [] del=' ',hex_string ='0x4d 0x42'; char [][] str; str = split(hex_string,del); printf("%.*s\n",str[0]); by = cast(byte) str[0]; buf.write(cast(byte)by); // Here is the problem printf("buf = '%.*s'\n", buf.toString()); return 0; } By the way, the linux port from dmd works nice. Regards Manfred |
May 18, 2003 Re: from char[] to byte | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Hansen | Manfred Hansen wrote: > Hi, > > how can i cast from char[] to byte? I don't understand what kind of conversion result you expect. > import outbuffer; > > int main () { > //byte by = 0x4d; > byte by; > OutBuffer buf = new OutBuffer(); > char [] del=' ',hex_string ='0x4d 0x42'; > char [][] str; So "str" is an array of strings. > > str = split(hex_string,del); > printf("%.*s\n",str[0]); > by = cast(byte) str[0]; you're converting an *array* (a string) into a *value* - which makes no sense. Have you meant: by = cast(byte) (str[0][0]); > buf.write(cast(byte)by); // Here is the problem why here? "by" is a byte, casting it to byte is redundant. > printf("buf = '%.*s'\n", buf.toString()); > return 0; > } Thus modified proggie gives following output: 0x4d buf = '0' -i. |
May 18, 2003 Re: from char[] to byte | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Minkov | Ilya Minkov wrote: > Manfred Hansen wrote: >> Hi, >> >> how can i cast from char[] to byte? > > I don't understand what kind of conversion result you expect. I want the output 'M' . 4d is the ASCII code for M. In my real programm i will sent hex number's to the socket for communication to the mysql server (linux), but i believe this is a long way. > >> import outbuffer; >> >> int main () { >> //byte by = 0x4d; >> byte by; >> OutBuffer buf = new OutBuffer(); >> char [] del=' ',hex_string ='0x4d 0x42'; >> char [][] str; > > So "str" is an array of strings. > >> >> str = split(hex_string,del); >> printf("%.*s\n",str[0]); >> by = cast(byte) str[0]; > > you're converting an *array* (a string) into a *value* - which makes no > sense. > > Have you meant: > > by = cast(byte) (str[0][0]); > >> buf.write(cast(byte)by); // Here is the problem > > why here? "by" is a byte, casting it to byte is redundant. Oh, yes this was wrong from me. I mean'd this line " by = cast(byte) str[0];" > >> printf("buf = '%.*s'\n", buf.toString()); >> return 0; >> } > > Thus modified proggie gives following output: > > 0x4d > buf = '0' That's not what i want. I will get buf = 'M' . > > -i. Regards Manfred |
May 18, 2003 Re: from char[] to byte | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manfred Hansen | Manfred Hansen wrote: > I want the output 'M' . > 4d is the ASCII code for M. > In my real programm i will sent hex number's to the socket for communication to the mysql server (linux), but i believe this is a long way. Hmm... hex numbers? Well, as you wish. I know nothing about MySQL. > Oh, yes this was wrong from me. I mean'd this line " by = cast(byte) str[0];" str is an *array* of *strings*. It is like you have a sheet of lined paper, on which you must first select a line, then a column. That is, you want to take one character like: by = str[line][column]; //add a cast if it complains str[x] would give you the whole line of charcters. Also think whether a string would be more appropriate ("char [] str;") - i'm still unclear about what you want and how you intend it to work, bit the name "str" suggests it being a string, not an array of strings. Then, if you want to print a byte as a character, why don't you cast it into a character? Consider: "buf.write" resolves to different functions depending on a formal data type. -i. |
Copyright © 1999-2021 by the D Language Foundation