HELP PLEASE - How to convert string to byte array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpptutor2000@yahoo.com

    HELP PLEASE - How to convert string to byte array

    Could some C guru help me please? A byte is an unsigned char in C.
    How do I convert from a C string to a corresponding byte array. Any
    help would be greatly appreciated.

  • Joona I Palaste

    #2
    Re: HELP PLEASE - How to convert string to byte array

    cpptutor2000@ya hoo.com scribbled the following:[color=blue]
    > Could some C guru help me please? A byte is an unsigned char in C.
    > How do I convert from a C string to a corresponding byte array. Any
    > help would be greatly appreciated.[/color]

    You don't need to "convert" it. A C string can be used like a byte
    array.

    For example:
    char *s = "Hello, world!";
    char c = s[0]; /* c now contains 'H' */

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "Bad things only happen to scoundrels."
    - Moominmamma

    Comment

    • Martin Ambuhl

      #3
      Re: HELP PLEASE - How to convert string to byte array

      cpptutor2000@ya hoo.com wrote:[color=blue]
      > Could some C guru help me please? A byte is an unsigned char in C.[/color]

      No, 'byte' is a synonym for 'char', which may be either signed or
      unsigned. An unsigned byte is an unsigned char. Whether there is any
      type that corresponds to, for example, an octet depends upon the
      implementation.
      [color=blue]
      > How do I convert from a C string to a corresponding byte array. Any
      > help would be greatly appreciated.[/color]

      A C string *is* a zero-terminated byte array.

      char foo[] = "a string";

      foo is a string and a char (or byte) array containing
      {'a', ' ', 's', 't', 'r', 'i', 'n', 'g', 0}



      Comment

      • Ari Lukumies

        #4
        Re: HELP PLEASE - How to convert string to byte array

        Martin Ambuhl wrote:[color=blue]
        > cpptutor2000@ya hoo.com wrote:
        >[color=green]
        >> Could some C guru help me please? A byte is an unsigned char in C.[/color]
        >
        > No, 'byte' is a synonym for 'char', which may be either signed or
        > unsigned. An unsigned byte is an unsigned char. Whether there is any
        > type that corresponds to, for example, an octet depends upon the
        > implementation.[/color]

        What? Have I been away too long? I thought that a char is an entity
        of value, but not necessarily one that resembles a byte. Maybe
        I've missed a standard... :D

        -atl-
        --
        A multiverse is figments of its own creations

        Comment

        • Martin Ambuhl

          #5
          Re: HELP PLEASE - How to convert string to byte array

          Ari Lukumies wrote:[color=blue]
          > Martin Ambuhl wrote:
          >[color=green]
          >> cpptutor2000@ya hoo.com wrote:
          >>[color=darkred]
          >>> Could some C guru help me please? A byte is an unsigned char in C.[/color]
          >>
          >>
          >> No, 'byte' is a synonym for 'char', which may be either signed or
          >> unsigned. An unsigned byte is an unsigned char. Whether there is any
          >> type that corresponds to, for example, an octet depends upon the
          >> implementation.[/color]
          >
          >
          > What? Have I been away too long? I thought that a char is an entity
          > of value, but not necessarily one that resembles a byte. Maybe
          > I've missed a standard... :D[/color]

          I have no idea what you think you said. A byte and a char are the same
          by definition in C, and has been for 16 years, so you've missed not *a*
          standard, but *every* standard.

          Here's what a char is (3.1.2.5 in C89)
          An object declared as type char is large enough to store any member
          of the basic execution character set. If a member of the required
          source character set enumerated in $2.2.1 is stored in a char object,
          its value is guaranteed to be positive. If other quantities are
          stored in a char object, the behavior is implementation-defined: the
          values are treated as either signed or nonnegative integers.

          Here's what a byte is (1.6 in C89)
          * Byte --- the unit of data storage in the execution environment
          large enough to hold any member of the basic character set of the
          execution environment. It shall be possible to express the address of
          each individual byte of an object uniquely. A byte is composed of a
          contiguous sequence of bits, the number of which is
          implementation-defined. The least significant bit is called the
          low-order bit; the most significant bit is called the high-order bit.

          and if that's not clear, note from the language for sizeof (3.3.3.4);
          The sizeof operator yields the size (in bytes) of its operand, ...
          When applied to an operand that has type char , unsigned char , or
          signed char , (or a qualified version thereof) the result is 1

          Comment

          • pete

            #6
            Re: HELP PLEASE - How to convert string to byte array

            Ari Lukumies wrote:[color=blue]
            >
            > Martin Ambuhl wrote:[color=green]
            > > cpptutor2000@ya hoo.com wrote:
            > >[color=darkred]
            > >> Could some C guru help me please? A byte is an unsigned char in C.[/color]
            > >
            > > No, 'byte' is a synonym for 'char', which may be either signed or
            > > unsigned. An unsigned byte is an unsigned char.
            > > Whether there is any
            > > type that corresponds to, for example, an octet depends upon the
            > > implementation.[/color]
            >
            > What? Have I been away too long? I thought that a char is an entity
            > of value, but not necessarily one that resembles a byte. Maybe
            > I've missed a standard... :D[/color]

            A byte is a unit of memory.
            char is an object type.

            --
            pete

            Comment

            • Joona I Palaste

              #7
              Re: HELP PLEASE - How to convert string to byte array

              pete <pfiland@mindsp ring.com> scribbled the following:[color=blue]
              > Ari Lukumies wrote:[color=green]
              >> Martin Ambuhl wrote:[color=darkred]
              >> > cpptutor2000@ya hoo.com wrote:
              >> >> Could some C guru help me please? A byte is an unsigned char in C.
              >> >
              >> > No, 'byte' is a synonym for 'char', which may be either signed or
              >> > unsigned. An unsigned byte is an unsigned char.
              >> > Whether there is any
              >> > type that corresponds to, for example, an octet depends upon the
              >> > implementation.[/color]
              >>
              >> What? Have I been away too long? I thought that a char is an entity
              >> of value, but not necessarily one that resembles a byte. Maybe
              >> I've missed a standard... :D[/color][/color]
              [color=blue]
              > A byte is a unit of memory.
              > char is an object type.[/color]

              In the context of the C programming language, "byte" and "char" are the
              same thing. In other contexts they might be different.

              --
              /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
              \-------------------------------------------------------- rules! --------/
              "C++ looks like line noise."
              - Fred L. Baube III

              Comment

              • Joe Wright

                #8
                Re: HELP PLEASE - How to convert string to byte array

                Joona I Palaste wrote:[color=blue]
                > pete <pfiland@mindsp ring.com> scribbled the following:
                >[color=green]
                >>Ari Lukumies wrote:
                >>[color=darkred]
                >>>Martin Ambuhl wrote:
                >>>
                >>>>cpptutor200 0@yahoo.com wrote:
                >>>>
                >>>>>Could some C guru help me please? A byte is an unsigned char in C.
                >>>>
                >>>>No, 'byte' is a synonym for 'char', which may be either signed or
                >>>>unsigned. An unsigned byte is an unsigned char.
                >>>>Whether there is any
                >>>>type that corresponds to, for example, an octet depends upon the
                >>>>implementat ion.
                >>>
                >>>What? Have I been away too long? I thought that a char is an entity
                >>>of value, but not necessarily one that resembles a byte. Maybe
                >>>I've missed a standard... :D[/color][/color]
                >
                >[color=green]
                >>A byte is a unit of memory.
                >>char is an object type.[/color]
                >
                >
                > In the context of the C programming language, "byte" and "char" are the
                > same thing. In other contexts they might be different.
                >[/color]

                I agree. Now if we can get Chris Torek to agree ..

                --
                Joe Wright mailto:joewwrig ht@comcast.net
                "Everything should be made as simple as possible, but not simpler."
                --- Albert Einstein ---

                Comment

                • Richard Bos

                  #9
                  Re: HELP PLEASE - How to convert string to byte array

                  Joe Wright <joewwright@com cast.net> wrote:
                  [color=blue]
                  > Joona I Palaste wrote:[color=green]
                  > > pete <pfiland@mindsp ring.com> scribbled the following:
                  > >[color=darkred]
                  > >>A byte is a unit of memory.
                  > >>char is an object type.[/color]
                  > >
                  > > In the context of the C programming language, "byte" and "char" are the
                  > > same thing. In other contexts they might be different.[/color]
                  >
                  > I agree. Now if we can get Chris Torek to agree ..[/color]

                  Well, if you're being picky, pete is right: char is a basic object type;
                  a byte is the amount of memory that one object of type char takes.

                  Richard

                  Comment

                  • Joe Wright

                    #10
                    Re: HELP PLEASE - How to convert string to byte array

                    Richard Bos wrote:[color=blue]
                    > Joe Wright <joewwright@com cast.net> wrote:
                    >
                    >[color=green]
                    >>Joona I Palaste wrote:
                    >>[color=darkred]
                    >>>pete <pfiland@mindsp ring.com> scribbled the following:
                    >>>
                    >>>
                    >>>>A byte is a unit of memory.
                    >>>>char is an object type.
                    >>>
                    >>>In the context of the C programming language, "byte" and "char" are the
                    >>>same thing. In other contexts they might be different.[/color]
                    >>
                    >>I agree. Now if we can get Chris Torek to agree ..[/color]
                    >
                    >
                    > Well, if you're being picky, pete is right: char is a basic object type;
                    > a byte is the amount of memory that one object of type char takes.
                    >
                    > Richard[/color]

                    Maybe a difference without a distinction. The term char is a C language
                    thing and the term byte is more generic, having to do with memory size
                    and disk size, etc. For most practical purposes the C char type
                    describes a byte object in memory and on disk. Some might suggest a
                    difference between a char object and a byte object. I am not one of them.

                    --
                    Joe Wright mailto:joewwrig ht@comcast.net
                    "Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

                    Comment

                    • Lawrence Kirby

                      #11
                      Re: HELP PLEASE - How to convert string to byte array

                      On Mon, 06 Jun 2005 19:28:04 -0400, Joe Wright wrote:
                      [color=blue]
                      > Richard Bos wrote:[color=green]
                      >> Joe Wright <joewwright@com cast.net> wrote:
                      >>
                      >>[color=darkred]
                      >>>Joona I Palaste wrote:
                      >>>
                      >>>>pete <pfiland@mindsp ring.com> scribbled the following:
                      >>>>
                      >>>>
                      >>>>>A byte is a unit of memory.
                      >>>>>char is an object type.
                      >>>>
                      >>>>In the context of the C programming language, "byte" and "char" are the
                      >>>>same thing. In other contexts they might be different.
                      >>>
                      >>>I agree. Now if we can get Chris Torek to agree ..[/color]
                      >>
                      >>
                      >> Well, if you're being picky, pete is right: char is a basic object type;
                      >> a byte is the amount of memory that one object of type char takes.
                      >>
                      >> Richard[/color]
                      >
                      > Maybe a difference without a distinction. The term char is a C language
                      > thing and the term byte is more generic, having to do with memory size
                      > and disk size, etc. For most practical purposes the C char type
                      > describes a byte object in memory and on disk. Some might suggest a
                      > difference between a char object and a byte object. I am not one of them.[/color]

                      As the standard defines them char is a type and byte is a typeless unit of
                      memory allocation. Although strongly related (character types are defined
                      to have a size of 1 byte) they are different concepts.

                      Note that in the description above there is no such thing in C as a "byte
                      object" (objects always have a type), a char object is a byte *sized*
                      object.

                      Lawrence

                      Comment

                      • pete

                        #12
                        Re: HELP PLEASE - How to convert string to byte array

                        Lawrence Kirby wrote:
                        [color=blue]
                        > Note that in the description above there is no
                        > such thing in C as a "byte
                        > object" (objects always have a type),
                        > a char object is a byte *sized* object.[/color]

                        An allocated object doesn't always have a type.

                        --
                        pete

                        Comment

                        • Mark McIntyre

                          #13
                          Re: HELP PLEASE - How to convert string to byte array

                          On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
                          <pfiland@mindsp ring.com> wrote:
                          [color=blue]
                          >Lawrence Kirby wrote:
                          >[color=green]
                          >> Note that in the description above there is no
                          >> such thing in C as a "byte
                          >> object" (objects always have a type),
                          >> a char object is a byte *sized* object.[/color]
                          >
                          >An allocated object doesn't always have a type.[/color]

                          Can you provide an example?


                          --
                          Mark McIntyre
                          CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                          CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                          ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                          Comment

                          • Ben Pfaff

                            #14
                            Re: HELP PLEASE - How to convert string to byte array

                            Mark McIntyre <markmcintyre@s pamcop.net> writes:
                            [color=blue]
                            > On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
                            > <pfiland@mindsp ring.com> wrote:
                            >[color=green]
                            >>An allocated object doesn't always have a type.[/color]
                            >
                            > Can you provide an example?[/color]

                            The object that a non-null pointer returned by malloc() points to
                            initially has no type.
                            --
                            "A lesson for us all: Even in trivia there are traps."
                            --Eric Sosman

                            Comment

                            • pete

                              #15
                              Re: HELP PLEASE - How to convert string to byte array

                              Mark McIntyre wrote:[color=blue]
                              >
                              > On Sat, 11 Jun 2005 23:44:45 GMT, in comp.lang.c , pete
                              > <pfiland@mindsp ring.com> wrote:
                              >[color=green]
                              > >Lawrence Kirby wrote:
                              > >[color=darkred]
                              > >> Note that in the description above there is no
                              > >> such thing in C as a "byte
                              > >> object" (objects always have a type),
                              > >> a char object is a byte *sized* object.[/color]
                              > >
                              > >An allocated object doesn't always have a type.[/color]
                              >
                              > Can you provide an example?[/color]

                              /* BEGIN new.c */

                              #include <stdio.h>
                              #include <stdlib.h>

                              int main(void)
                              {
                              void *pointer;

                              pointer = malloc(1);
                              printf("If %p", pointer);
                              puts(
                              " isn't a null pointer, then it's the address of an\n"
                              "allocated object that doesn't have a type."
                              );
                              free(pointer);
                              return 0;
                              }

                              /* END new.c */

                              --
                              pete

                              Comment

                              Working...