Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to convert an unsigned char to byte

Reply
Thread Tools

how to convert an unsigned char to byte

 
 
QQ
Guest
Posts: n/a
 
      09-28-2005
I know a char is 2 bytes, the conversion is like
byte[] byte_array = new byte[2*char_length]; //Allocate double mem as
that of char
then for each char do
byte[0] = (byte) char[0] & 0xff
byte[1] = (byte)( char[0] >> 8 & 0xff)

one unsigned char is 1 byte, could anyone tell me the conversion
method?

Thank you very much!

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-28-2005
"QQ" <(E-Mail Removed)> wrote in news:1127927753.715838.327590
@o13g2000cwo.googlegroups.com:

> I know a char is 2 bytes,


In C, sizeof(char) is always 1.

> the conversion is like
> byte[] byte_array = new byte[2*char_length];


Are you under the impression that you are in a Java group?

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      09-28-2005
QQ wrote:
> I know a char is 2 bytes, the conversion is like


Not in C it isn't. In C a char is 1 byte by definition, although that
byte can be more than 8 bits.

> byte[] byte_array = new byte[2*char_length]; //Allocate double mem as


<snip>

The above is not C. It could be C++ (comp.lang.c++ is the news group
next door) but I believe that in C++ a char is 1 byte by definition as well.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-28-2005
QQ wrote:
> I know a char is 2 bytes,


A char is 1 byte by definition. Anything following from your incorrect
premise is either false or only true by luck.

> the conversion is like
> byte[] byte_array = new byte[2*char_length]; //Allocate double mem as


the above line -- apart from its use of the C++-inspired syntax error
'new' -- makes no sense.
If there were a type 'byte' (which there isn't), a declaration of
byte_array would look like
byte byte_array[2 * char_length];
Of course the identifier 'char_length' is meant only to confuse. If
char_length == sizeof(char), then it should be omitted, since it is 1 by
definition. Otherwise, it needs a new name.

> that of char
> then for each char do
> byte[0] = (byte) char[0] & 0xff


Not only is there no type 'byte', you are using 'byte' as the name of a
variable and as a putative type. Further, you are using the name of an
actual type 'char' as the name of a variable. It is a wonder that you
can find your way to the keyboard when you strew such misdirecting
tokens over the landscape.

> byte[1] = (byte)( char[0] >> 8 & 0xff)
>
> one unsigned char is 1 byte, could anyone tell me the conversion
> method?


Try again. Next time spend at least enough time to ask a coherent question.

> Thank you very much!
>

 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      09-28-2005
QQ wrote on 28/09/05 :
> I know a char is 2 bytes, the conversion is like


What ? An unsigned char is very likely a byte to me. BTW, there is no
'byte' type in C. Maybe, on your implementation or application, you
have a byte type that have the size of 2 char, but it's very stressy.

> byte[] byte_array = new byte[2*char_length]; //Allocate double mem as


Wait a minute. We are takling C here. If you want C++, please knock
next door.



--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.


 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      09-28-2005
Flash Gordon wrote:
> QQ wrote:
>
>> I know a char is 2 bytes, the conversion is like

>
>
> Not in C it isn't. In C a char is 1 byte by definition, although that
> byte can be more than 8 bits.
>

But, for completeness, not less. And see below -- a char *may* be two
bytes, depending on how you define "byte". (How the OP defines it is
anyone's guess.)

>> byte[] byte_array = new byte[2*char_length]; //Allocate double mem as

>
>
> <snip>
>
> The above is not C. It could be C++ (comp.lang.c++ is the news group
> next door) but I believe that in C++ a char is 1 byte by definition as
> well.


*Provided* you use the definition of "byte" as supplied by the standard,
not as "the smallest memory unit my machine can address" (a quite
popular alternative), which is almost always but not necessarily a C
byte. It's legal for an implementation to use a 16-bit char composed of
2 8-bit machine bytes. (Disclaimer: IANALL.)

S.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      09-29-2005
On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
<(E-Mail Removed)> wrote in comp.lang.c:

> Flash Gordon wrote:
> > QQ wrote:
> >
> >> I know a char is 2 bytes, the conversion is like

> >
> >
> > Not in C it isn't. In C a char is 1 byte by definition, although that
> > byte can be more than 8 bits.
> >

> But, for completeness, not less. And see below -- a char *may* be two
> bytes, depending on how you define "byte". (How the OP defines it is
> anyone's guess.)


No, it may not, not in C and not among actually literate computer
professionals.

If a char contains more than 8 bits, it is certainly larger than on
"octet", and may indeed be two or even four "octets" in size.

But it is still one byte in C, by definition.

If you are not talking C, feel free to go elsewhere.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      09-29-2005
Jack Klein wrote:
> On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
> <(E-Mail Removed)> wrote in comp.lang.c:
>
>
>>Flash Gordon wrote:
>>
>>>QQ wrote:
>>>
>>>
>>>>I know a char is 2 bytes, the conversion is like
>>>
>>>
>>>Not in C it isn't. In C a char is 1 byte by definition, although that
>>>byte can be more than 8 bits.
>>>

>>
>>But, for completeness, not less. And see below -- a char *may* be two
>>bytes, depending on how you define "byte". (How the OP defines it is
>>anyone's guess.)

>
>
> No, it may not, not in C and not among actually literate computer
> professionals.
>
> If a char contains more than 8 bits, it is certainly larger than on
> "octet", and may indeed be two or even four "octets" in size.
>
> But it is still one byte in C, by definition.
>
> If you are not talking C, feel free to go elsewhere.
>

I'm going to assume you did actually *read* the part I referred to with
"see below", and you so deftly eliminated from context. Which can only
mean you got so enraged at my audacity to dare talk about "byte" in the
non C way that you didn't bother to address the point it made.

The question of my literacy I'll leave for another day.

Jeez. "Thank you, sir, may I have another?"

S.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-29-2005
Skarmander <(E-Mail Removed)> writes:
> Jack Klein wrote:
>> On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
>> <(E-Mail Removed)> wrote in comp.lang.c:

[...]
>>> But, for completeness, not less. And see below -- a char *may* be
>>> two bytes, depending on how you define "byte". (How the OP defines
>>> it is anyone's guess.)

>> No, it may not, not in C and not among actually literate computer
>> professionals.
>> If a char contains more than 8 bits, it is certainly larger than on
>> "octet", and may indeed be two or even four "octets" in size.
>> But it is still one byte in C, by definition.
>> If you are not talking C, feel free to go elsewhere.
>>

> I'm going to assume you did actually *read* the part I referred to
> with "see below", and you so deftly eliminated from context. Which can
> only mean you got so enraged at my audacity to dare talk about "byte"
> in the non C way that you didn't bother to address the point it made.
>
> The question of my literacy I'll leave for another day.
>
> Jeez. "Thank you, sir, may I have another?"


Without commenting on whether Jack was overly harsh, I'll offer some
advice.

In this newsgroup, the unqualified word "byte" *always* refers to the
term as defined by the C standard. If you want to talk about
something else that's called a "byte" in some other context, it would
be an excellent idea to qualify the term.

For example, a system may define the term "byte" differently than the
way C uses the term. A C char is always exactly one C byte, but it
might consist of two or more "system bytes", or a "system byte" might
consist of two or more C bytes.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      09-29-2005
Keith Thompson wrote:
> Skarmander <(E-Mail Removed)> writes:
>
>>Jack Klein wrote:
>>
>>>On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
>>><(E-Mail Removed)> wrote in comp.lang.c:

>
> [...]
>
>>>>But, for completeness, not less. And see below -- a char *may* be
>>>>two bytes, depending on how you define "byte". (How the OP defines
>>>>it is anyone's guess.)
>>>
>>>No, it may not, not in C and not among actually literate computer
>>>professionals.
>>>If a char contains more than 8 bits, it is certainly larger than on
>>>"octet", and may indeed be two or even four "octets" in size.
>>>But it is still one byte in C, by definition.
>>>If you are not talking C, feel free to go elsewhere.
>>>

>>
>>I'm going to assume you did actually *read* the part I referred to
>>with "see below", and you so deftly eliminated from context. Which can
>>only mean you got so enraged at my audacity to dare talk about "byte"
>>in the non C way that you didn't bother to address the point it made.
>>
>>The question of my literacy I'll leave for another day.
>>
>>Jeez. "Thank you, sir, may I have another?"

>
>
> Without commenting on whether Jack was overly harsh, I'll offer some
> advice.
>
> In this newsgroup, the unqualified word "byte" *always* refers to the
> term as defined by the C standard. If you want to talk about
> something else that's called a "byte" in some other context, it would
> be an excellent idea to qualify the term.
>
> For example, a system may define the term "byte" differently than the
> way C uses the term. A C char is always exactly one C byte, but it
> might consist of two or more "system bytes", or a "system byte" might
> consist of two or more C bytes.
>

Enough! Let me resurrect the part that was excised by mr. Klein:

>>> byte[] byte_array = new byte[2*char_length]; //Allocate double
>>> mem as

>>
>>
>>
>> <snip>
>>
>> The above is not C. It could be C++ (comp.lang.c++ is the news
>> group next door) but I believe that in C++ a char is 1 byte by
>> definition as well.

>
>
> *Provided* you use the definition of "byte" as supplied by the
> standard, not as "the smallest memory unit my machine can address" (a
> quite popular alternative), which is almost always but not
> necessarily a C byte. It's legal for an implementation to use a
> 16-bit char composed of 2 8-bit machine bytes. (Disclaimer: IANALL.)


*That* is what I wrote. Was I somehow not being clear I was explicitly
considering non-C-standard definitions of "byte"? If so, I apologize,
but there seems to be a case of "I sensed stupidity so I stopped reading
at this point" going on here.

This part of the thread has outstayed its welcome as far as usefulness
goes, methinks. Let's bury it. It's obvious all involved have understood
whatever points were buried in it.

S.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
Linking error LNK2001 - "__declspec(dllimport) private: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Tidy(unsigned short)" (__imp_?_Tidy@?$basic_string@DU?$char_ sharmadeep1980@gmail.com C++ 1 07-07-2006 07:27 AM
How to byte[ ] --> char[ ] and char[ ] --> byte[ ]? Richard Java 11 02-01-2006 07:34 AM
void*, char*, unsigned char*, signed char* Steffen Fiksdal C Programming 1 05-09-2005 02:33 AM



Advertisments