473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get unicode character by bit shifting

Hello,

I have a pretty interesting problem here..

Ok, I have an integer that needs to be right shifted, and then
converted to a char.. which is then is used to build a string. Heres
an example of the steps:

long Value = 1667688574;

char c1 = Value >24; // produces '99' or 'c'
char c2 = Value >16; // produces '25446' or '’Í'
char c3 = Value >8; // generates OverflowException "Value was
either too large or too small for a character."
char c4 = Value >0; // never gets executed due to the error above

The error gets thrown using long, ulong, etc..
The same scenario in php produces 'c' 'f' '¨¨' and '~', respectively.
Any ideas as to what I may be doing wrong?
Jan 13 '08 #1
6 2830
I also forgot to mention that my goal is to duplicate the php scenario
that I mentioned above. The desired output is 'c' 'f' 'č' and '~'
Jan 13 '08 #2
On 13 Jan, 15:41, tcomer <tco...@gmail.comwrote:
I also forgot to mention that my goal is to duplicate the php scenario
that I mentioned above. The desired output is 'c' 'f' 'č' and '~'
Shifting down bits leaves the "upper" so Value >24 will be correct..
te other u just ger more than u want if not masking with & FF
//CY
Jan 13 '08 #3
ch*******@gmail.com wrote:
On 13 Jan, 15:41, tcomer <tco...@gmail.comwrote:
>I also forgot to mention that my goal is to duplicate the php scenario
that I mentioned above. The desired output is 'c' 'f' 'č' and '~'

Shifting down bits leaves the "upper" so Value >24 will be correct..
te other u just ger more than u want if not masking with & FF
The >24 will also fail if the high bit is set in Value (if it
is negative).

Arne
Jan 13 '08 #4
tcomer wrote:
Ok, I have an integer that needs to be right shifted, and then
converted to a char.. which is then is used to build a string. Heres
an example of the steps:

long Value = 1667688574;

char c1 = Value >24; // produces '99' or 'c'
char c2 = Value >16; // produces '25446' or '捦'
char c3 = Value >8; // generates OverflowException "Value was
either too large or too small for a character."
char c4 = Value >0; // never gets executed due to the error above

The error gets thrown using long, ulong, etc..
The same scenario in php produces 'c' 'f' 'è' and '~', respectively.
You have a long with a value that coul have been in an int.

Besides the correct suggestion to use & 0xFF, then you may
consider redesigning the logic a bit.

If you actually wants a text string with first byte as
first char, then you can use:

Encoding.Default.GetString(BitConverter.GetBytes(V alue))

Arne

Jan 13 '08 #5
>
The >24 will also fail if the high bit is set in Value (if it
is negative).

Arne
and long Value = 1667688574; looks negative?
ok, shifting it leaves 63(H), and setting the negative.. 163 and then
& FF and 63 again... *doh*
//CY
Jan 14 '08 #6
long Value = 1667688574;
....
Any ideas as to what I may be doing wrong?
What are you really trying to do?
Unicode characters go beyond 0xFF, taking more than one byte.
So, even if you & 0xFF as sugested, you risk damaging some
international stuff.
If you try some kind of encryption, then you probably
better go with a strong, standard thing.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Jan 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Hallvard B Furuseth | last post by:
Has someone got a Python routine or module which converts Unicode strings to lowercase (or uppercase)? What I actually need to do is to compare a number of strings in a case-insensitive manner,...
27
by: EU citizen | last post by:
Do web pages have to be created in unicode in order to use UTF-8 encoding? If so, can anyone name a free application which I can use under Windows 98 to create web pages?
6
by: S. | last post by:
if in my website i am using the sgml { notation, is it accurate to say to my users that the site uses unicode or that it requires unicode? is there a mathematical formula to calculate a unicode...
48
by: Zenobia | last post by:
Recently I was editing a document in GoLive 6. I like GoLive because it has some nice features such as: * rewrite source code * check syntax * global search & replace (through several files at...
4
by: Basil | last post by:
Hello. I have compiler BC Builder 6.0. I have an example: #include <strstrea.h> int main () { wchar_t ff = {' s','d ', 'f', 'g', 't'};
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
12
by: damjan | last post by:
This may look like a silly question to someone, but the more I try to understand Unicode the more lost I feel. To say that I am not a beginner C++ programmer, only had no need to delve into...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
7
by: 7stud | last post by:
Based on this example and the error: ----- u_str = u"abc\u9999" print u_str UnicodeEncodeError: 'ascii' codec can't encode character u'\u9999' in position 3: ordinal not in range(128) ------
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.