Home arrow static arrow Java Programming [Archive] - How can i convert char(16bits) to byte(8bits)
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How can i convert char(16bits) to byte(8bits)
This topic has 9 replies on 1 page.

Posts:4
Registered: 6/30/04
How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 5:49 PM



 
How can i convert char(16bits) to byte(8bits)
thanks!
 

Posts:2,206
Registered: 8/15/02
Re: How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 5:52 PM (reply 1 of 9)



 
Do you mind losing the top byte?
 

Posts:4
Registered: 6/30/04
Re: How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 5:56 PM (reply 2 of 9)



 
I don't want to lose the top byte?
 

Posts:458
Registered: 2/27/02
Re: How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 9:11 PM (reply 3 of 9)



 
byte b = (byte)aCHar;
 

Posts:2,909
Registered: 13.8.2003
Re: How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 10:31 PM (reply 4 of 9)



 
I don't want to lose the top byte?

You can't fit 16 bits into 8 bits. What do you want to do with the top byte?
 

Posts:4
Registered: 6/30/04
Re: How can i convert char(16bits) to byte(8bits)  
Aug 2, 2004 11:54 PM (reply 5 of 9)



 
char a='a';
byte b1,b2;
b1=(byte)(a&oxFF00)>>8;
b2=(byte)a;

how this?
 

Posts:5,965
Registered: 5/17/03
Re: How can i convert char(16bits) to byte(8bits)  
Aug 3, 2004 12:39 AM (reply 6 of 9)



 
The best is to check,
char c = (char)0xfeff;byte b1 = (byte)c;byte b2 = (byte)(c>>8);System.out.println(b1);System.out.println(b2);

The above prints -1 and -2 which is correct because the bitpattern 0xff interpreted as a byte is -1 (and 0xfe is -2). A byte is an 8 bit two's complement integer in Java.
 

Posts:4
Registered: 6/30/04
Re: How can i convert char(16bits) to byte(8bits)  
Aug 3, 2004 1:11 AM (reply 7 of 9)



 
The best is to check,
char c = (char)0xfeff;byte b1 = (byte)c;byte b2 = (byte)(c>>8);System.out.println(b1);System.out.println(b2);

The above prints -1 and -2 which is correct because
the bitpattern 0xff interpreted as a byte is -1 (and
0xfe is -2). A byte is an 8 bit two's complement
integer in Java.

thanks very much
 

Posts:5,965
Registered: 5/17/03
Re: How can i convert char(16bits) to byte(8bits)  
Aug 3, 2004 2:05 AM (reply 8 of 9)



 
I've just learnt that the most straightforward way to enter any Unicode char is to use the \u escape sequence followed by a hexadecimal number, like
char c = '\ufeff';
 

Posts:2,909
Registered: 13.8.2003
Re: How can i convert char(16bits) to byte(8bits)  
Aug 3, 2004 2:17 AM (reply 9 of 9)



 
I've just learnt that the most straightforward way to
enter any Unicode char is to use the \u escape

Then this day hasn't been a complete waste. Carry on young grasshopper. (No offense intended in any way btw :)
 
This topic has 9 replies on 1 page.