• Post Reply Bookmark Topic Watch Topic
  • New Topic

bytes to char  RSS feed

 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I was wondering how i can turn two bytes into a character.
What i think i need to do is to assign the two bytes into an int and then cast this int to a char.
However how do i create the int so that it reflects the correct char value. I know i need to use one/or two of the bit-wise operators. I need to take the b bits in the first byte and put them in positions 8 to 15 in the int. Then i need to take the 8 bits in the second byte and place them in positions 0 to 7 in the int. Can anyone tell me how exactly i should do this. My experimentation with the bit-wise operators has not been too successfull so far
thanks,
John
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can u give more data as to what ur 2 bytes are..
something that I can think of now is,
create a String using the byte[] constructor of String.And then do a String.charat(0) to get ur char.
I have not tested it to give a proper char but i suppose it should work.
Try it and let me(and evrybdy else) know,
Chinmay....
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Barry's code is giving me '?' always.
even my reply gives wrong results.
After getting the string,doa Interger.parseInt() on it and then typecast that int to char to get ur result.
So byte a = 6
byte b = 5
gives me 'A' as the o/p
Thanks.
Chinmay...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, it'b because of negatve valued bytes. I'm deleting the post until I fix it.
Lesson 1: test before posting code.
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chinmay Bajikar:
Hi,
Can u give more data as to what ur 2 bytes are..
something that I can think of now is,
create a String using the byte[] constructor of String.And then do a String.charat(0) to get ur char.
I have not tested it to give a proper char but i suppose it should work.
Try it and let me(and evrybdy else) know,
Chinmay....

No this created a two character string. The String class uses the default encoding of your system which in my case is cp1252(Windows encoding). This is an 8 bit encoding so the result is two characters and not the one that i need. I think bitwise manipulation is definately the way to do this but just not sure how exactly........
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this is a bit better
 
Chinmay Bajikar
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry,
ur code did not compile.
It requires me to cast to byte first.
and it is giving me a '?' mark again.
And Ryan..did u see my 2nd post.
Is that what u want?
Chinmay.....
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
Maybe this is a bit better

this is perfect. Only mistake is the value assigned to b2 has to be between -128 and 127.
cheers,
john
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, the assignments to the bytes I added by hand after cutting the lines from my test program. Forgot the (byte) casts, sorry.
However in general the contents of the bytes can be anything between the bit patterns 0x00 and 0xFF. You will get them from some byte array buffer I guess.
-Barry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!