ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Dear Sir,
i want know how to change char or string into unsigned short.
Actualy i want to store a string into 2 bytes data type variable of pointer.
Like unsigned short s[]={'H','e','l','l','o',0};
it will work
but if i want to store a string into unsigned short or into 2 bytes data type ....
then what i should do???
char is a 1 byte value (usually). so in theory you can put two bytes into one unsigned short.
if char[0]=, for example, 67, and char[1]=68, then try
unsigned short int x = 256*char[0]+char[1]. Should work.
Important: you must use a single character or a single character array for this to work (char something[]). Don't use char *something[], cause thats a \n (newline, char 10) termintated string, not a character that can be represented by a single ascii byte.
If I understand you properly then I'd have to say that you can't achieve what you want to do. The reason being that you are trying to fit too much into the space allocated.
If you consider a variable like a suitcase, and data represents the cloths you want to store, you can only fit so many cloths into the suitcase, you might be able to cram a little more in but there comes a point when it's just not possible to add any more and you need to go out and get some more suitcases. In you example each char is designed to hold one character, so trying to hold more is going to lead to problems (you can fit a few more in with compression but there are limits which you will reach very quickly)
However, if you could expand on what it is you are trying to achieve then we might be able to help you.
What language is it, C or C++? In C it will be a little bit harder to achieve; you have to convert it by hand like "ichrispa" already suggested. In C++ you have a new data type named 'wchar_t' which is two bytes long and made for holding wide characters. To assign a wide string to such a type may look like this:
Code:
const wchar_t *wstr = L"Hello World";
Important: Don't forget the 'L' before a string; this indicates a wide string (16-bit chars)!
Ok, wchar_t is very platform dependent (am I the only one who thinks this is stupid??) However, most compilers provide an option to change the size of this type. If you are using g++, compile with '-fshort-wchar'. Example:
I'm obviously not understanding this thread because I don't know why the wchar data type is being introduced. The purpose of wchar is to support UNICODE characters which require more than one byte to be represented.
So going to the original question what is it you are trying to achieve by storing a string in a unsigned short? What do you intend to do with this unsigned short once you have squeezed the data into it?
...The purpose of wchar is to support UNICODE characters which require more than one byte to be represented.
Yes, I think that is exactly what "krishnacins" wants to do (please correct me if I'm wrong): A string with 16-bit characters. I thought a UNICODE-character was 16bit and platform indepandent, but it is not; sometimes it is 16 bit and sometimes 32 bit. But as I said before, g++ can switch wchar_t to be the desired 16 bit (like unsigned short int). Although this option has to be used with care: See "man g++" for details.
There are different versions of Unicode, utf8 is probably the most common encoding and will vary from 1 to 4 bytes depending upon the script used - so the latin script is typically 1 byte whilst variants of the chinese script will require four bytes to represent it.
The difference between "ASCII" and "UTF-8"; between "char *" and "wide characters" and "multibyte characters" etc can be VERY confusing; internationalization support can be VERY inconsistent from platform to platform, compiler to compiler, and library to library.
Hello,
i want to convert a string (str) into wchar_t (wch) & i want that g++ compiler wiil change the size of wchar_t varieble from 32 bts into 16 bits.So im using as g++ -fshort-wchar file name. Its working if im using
as wchar * wch =L"Hello";
but im using below function who takes one string variable & return wchar pointer.
if (size == (size_t) -1)
return NULL;
return buf;
}
It returns wchar_t variable or pointer which has 32 bits size.
& g++ -fshort-wchar this commond doesnt change the size of wchar from 32 bits to 16 bits. coz i think im unsig STL function c_str();
So please can anybody help me to come out from this prob that how can i use wchar_t varible of 16 bites.... with using above function
Last edited by krishnacins; 03-28-2006 at 03:59 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.