Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to convert an unsigned char to byte (http://www.velocityreviews.com/forums/t439573-how-to-convert-an-unsigned-char-to-byte.html)

 QQ 09-28-2005 05:15 PM

how to convert an unsigned char to byte

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!

 A. Sinan Unur 09-28-2005 06:37 PM

Re: how to convert an unsigned char to byte

"QQ" <junciu@yahoo.com> 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 <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

 Flash Gordon 09-28-2005 06:38 PM

Re: how to convert an unsigned char to byte

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.

 Martin Ambuhl 09-28-2005 06:46 PM

Re: how to convert an unsigned char to byte

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

 Emmanuel Delahaye 09-28-2005 07:03 PM

Re: how to convert an unsigned char to byte

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.

 Skarmander 09-28-2005 10:40 PM

Re: how to convert an unsigned char to byte

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.

 Jack Klein 09-29-2005 04:05 AM

Re: how to convert an unsigned char to byte

On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
<invalid@dontmailme.com> 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

 Skarmander 09-29-2005 11:56 AM

Re: how to convert an unsigned char to byte

Jack Klein wrote:
> On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
> <invalid@dontmailme.com> 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.

 Keith Thompson 09-29-2005 07:58 PM

Re: how to convert an unsigned char to byte

Skarmander <invalid@dontmailme.com> writes:
> Jack Klein wrote:
>> On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
>> <invalid@dontmailme.com> 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) kst-u@mib.org <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.

 Skarmander 09-29-2005 11:37 PM

Re: how to convert an unsigned char to byte

Keith Thompson wrote:
> Skarmander <invalid@dontmailme.com> writes:
>
>>Jack Klein wrote:
>>
>>>On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
>>><invalid@dontmailme.com> 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.

All times are GMT. The time now is 05:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.