Forums Register Login

bytes to char

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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........
+Pie Number of slices to send: Send
Maybe this is a bit better
+Pie Number of slices to send: Send
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.....
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 682 times.
Similar Threads
alternative to "ints" (?)
Java & C++ pro question
bitwise operators
int to bytes
Help! Can you explain these bitwise operations?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:22:05.