LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2006, 04:50 AM   #1
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Rep: Reputation: 0
How to change char or string to unsigned short


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???

Thanks
Krishna
 
Old 03-27-2006, 05:01 AM   #2
ichrispa
Member
 
Registered: Mar 2005
Location: Dresden, Germany
Distribution: OpenSuse 11.2/3, Debian 5.0 , Debian 1.3.1, OpenBSD
Posts: 277

Rep: Reputation: 32
No need for formalities, we're all equal here.

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.

Last edited by ichrispa; 03-27-2006 at 05:04 AM.
 
Old 03-27-2006, 05:16 AM   #3
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
But i want to store a string into unsigned short
Like:
char *ch="hello";
I want to store the value of ch into a unsigned short.

Like: unsigned short sh or *sh;
how can i operate it that the value of sh should be "hello";

Thanks
Krishna
 
Old 03-27-2006, 07:07 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Hi,

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.

graeme.
 
Old 03-27-2006, 07:49 AM   #5
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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)!
 
Old 03-27-2006, 08:47 AM   #6
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
Im using C++ & linux
But Flesym, size of wchar_t 4 bytes in linux. i want to store it in 2 bytes storage's variale.

Thanks
 
Old 03-27-2006, 09:44 AM   #7
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
Originally Posted by krishnacins
But Flesym, size of wchar_t 4 bytes in linux. i want to store it in 2 bytes storage's variale.
No, size of wchar_t is 2 bytes. Try this:
Code:
#include <iostream>
int main(){
  std::cout << "Size of wchar_t: " << sizeof(wchar_t);
  return 0;
}
In the output, you will see that it is "2".

I guess you checked the size of "wchar_t *"? -A pointer is of course 4 bytes as long as you have a 32-bit address space.

<EDIT>
Oh, I just tried it under Linux and you are right, it is indeed 4 bytes..., mmmh, i'll check it out
</EDIT>

Last edited by Flesym; 03-27-2006 at 09:57 AM.
 
Old 03-27-2006, 10:18 AM   #8
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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:
Code:
g++ test.cpp -fshort-wchar
This will change the size to 16 bit.

Hope that helped
-flesym
 
Old 03-27-2006, 12:54 PM   #9
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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?
 
Old 03-27-2006, 01:26 PM   #10
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Quote:
Originally Posted by graemef
...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.
 
Old 03-27-2006, 01:31 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 03-27-2006, 01:33 PM   #12
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Here's a great article that might help:

Quote:
"Linux Unicode Programming: How to Incorporate and Utilize Unicode for Foreign Language Support", Thomas Burger:
http://www-128.ibm.com/developerwork.../l-linuni.html
PS:
As Flesym said, if you want a Unicode string constant, then the "L" modifier is the way to do it:
Quote:
Code:
wchar_t *s = L"Hello World";
If you want to insure wchar_t is 16 bits, then
Quote:
Code:
"gcc ... -fshort-wchar"
is arguably the best way to do it (at least with gcc/g++).

Last edited by paulsm4; 03-27-2006 at 01:41 PM.
 
Old 03-28-2006, 03:56 AM   #13
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
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.

wchar_t * New_mbstowcs ( string str)
{
char *string = (char *) str.c_str();

size_t size = strlen (string) + 1;

wchar_t *buf = new wchar_t() ;

size = mbstowcs (buf, string, size);

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.
 
Old 03-28-2006, 04:19 AM   #14
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
thanks I have done it
 
Old 06-07-2007, 05:25 AM   #15
janBlue
LQ Newbie
 
Registered: Jun 2007
Posts: 1

Rep: Reputation: 0
Hello Krishnacins,
I need to know the solution, please because I have the same problem. Can you tell me this? thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
unsigned char in C language exvor Programming 16 03-21-2005 10:24 AM
unsigned char to wchar_t JurajPsycho Programming 6 03-19-2005 04:00 PM
Hex to unsigned char? george_mercury Programming 2 11-27-2004 01:27 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration