how to convert an unsigned char to byte

Q

QQ

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

Flash Gordon

QQ said:
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.
 
M

Martin Ambuhl

QQ said:
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.
 
E

Emmanuel Delahaye

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.
 
S

Skarmander

Flash said:
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.
 
J

Jack Klein

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.
 
S

Skarmander

Jack said:
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.
 
K

Keith Thompson

Skarmander said:
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.
 
S

Skarmander

Keith said:
Skarmander said:
[...]
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.
 
T

Tim Rentsch

Jack Klein said:
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.

The statement about how computer professionals use the term byte
doesn't match my experience. In most cases (80%, perhaps?), most
computer people I know use "byte" to mean 8 bits, or at least to mean
8 bits unless stated otherwise (eg, "10 bit bytes", as opposed to just
"bytes"). And the sorts of people I'm thinking of certainly would
qualify as literate.

Of course what Jack about the term "byte" is right in the context of
discussing Standard C. For a broader audience, however, it's reasonable
to expect that many of them will take "byte" to mean 8 bits unless there
is an explicit statement giving another meaning to the term.
 
O

Old Wolf

Emmanuel said:
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.

The statement is a syntax error in C++ . Try knocking on
some other newsgroup. (I can't recommend comp.lang.java.programmer,
which seems to have a worse S:N ratio than Mabden)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top