how to convert an unsigned char to byte

Discussion in 'C Programming' started by QQ, Sep 28, 2005.

  1. QQ

    QQ Guest

    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!
     
    QQ, Sep 28, 2005
    #1
    1. Advertising

  2. "QQ" <> 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 <>
    (reverse each component and remove .invalid for email address)
     
    A. Sinan Unur, Sep 28, 2005
    #2
    1. Advertising

  3. QQ

    Flash Gordon Guest

    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.
     
    Flash Gordon, Sep 28, 2005
    #3
  4. 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!
    >
     
    Martin Ambuhl, Sep 28, 2005
    #4
  5. 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.
     
    Emmanuel Delahaye, Sep 28, 2005
    #5
  6. QQ

    Skarmander Guest

    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.
     
    Skarmander, Sep 28, 2005
    #6
  7. QQ

    Jack Klein Guest

    On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
    <> 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 29, 2005
    #7
  8. QQ

    Skarmander Guest

    Jack Klein wrote:
    > On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
    > <> 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.
     
    Skarmander, Sep 29, 2005
    #8
  9. Skarmander <> writes:
    > Jack Klein wrote:
    >> On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
    >> <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 29, 2005
    #9
  10. QQ

    Skarmander Guest

    Keith Thompson wrote:
    > Skarmander <> writes:
    >
    >>Jack Klein wrote:
    >>
    >>>On Thu, 29 Sep 2005 00:40:06 +0200, Skarmander
    >>><> 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.
     
    Skarmander, Sep 30, 2005
    #10
  11. QQ

    Tim Rentsch Guest

    Jack Klein <> writes:

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


    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.
     
    Tim Rentsch, Oct 4, 2005
    #11
  12. QQ

    Old Wolf Guest

    Emmanuel Delahaye wrote:

    > 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)
     
    Old Wolf, Oct 5, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    11
    Views:
    1,085
    Adam Maass
    Feb 1, 2006
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    May 9, 2005
  3. Ioannis Vranos
    Replies:
    11
    Views:
    763
    Ioannis Vranos
    Mar 28, 2008
  4. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    617
    Ben Bacarisse
    Mar 29, 2008
  5. Alex Vinokur
    Replies:
    9
    Views:
    799
    James Kanze
    Oct 13, 2008
Loading...

Share This Page