tags:

views:

67

answers:

5

What is the difference between octet string and char ? how octet string can be used ? can anybody write a small C program on Octet string.How octet strings are stored in memory ?

+1  A: 
  • An octet is another word for a 8-bit byte.
  • A char is usually 8 bits, but may be another size on some architectures.
Sjoerd
signed char (signed being optional on most compilers, a changeable behavior) in ANSI C is not less than 8bits, not more than 8bits, and 8bits exactely. It's decimal representation, after 2-complements conversion, goes from -127 to 128. unsigned char goes from 0 to 255.
jpinto3912
@jpinto3912: it goes _at least_ from -127 to 127 or from 0 to 255, but it can cover a wider range. In fact, signed chars usually cover a wider range, from -128 to 127.
ninjalj
@jp: http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char
KennyTM
@ninjalj, thanks for pointing that, my bad... it was a speed-typo, I did meant -128 to 127, as in what you get from 2-complements on 8-bits. But you raised a very interesting and valid point: "at least -127", which is what you would get on a 1-complements storing computer.
jpinto3912
@Kenny, Ansi C has a default of char=8bits... apparently this can be changed (haven't checked, and wonder what mental conditions would trigger someone to change that). So char can (sic) be more than 8 bits. I eat my hat.
jpinto3912
@jp: All I know is ISO C requires `CHAR_BIT ≥ 8`. I don't know how it is in ANSI C.
KennyTM
@jpinto3912: The mental conditions nowadays typically involve writing/porting a C compiler for a 16-bit or 32-bit DSP. Also, ISO C supports 2-complement, 1-complement and sign-magnitude representations for integers.
ninjalj
@ninjalj, I think it's a bit mental to have the compiler treat char as more than 8bits, when we have int to play with. It's well understood that DSP/CPU memory word or instruction operands length might imply using 8bit var/const is less fast than using 16 or 32bit... but the compiler can make those padding/alignment changes as speed optimizations (i.e. use a whole 16/32bit mem-position and trunc at 8).
jpinto3912
A: 

An octet string is simply a sequence of bits grouped into chunks of 8. Those 8-sized groups often represent characters. Octet string is a basic data type used for SNMP.

Matt
+3  A: 

Standards (and such) use "octet" to explicitly state that they're talking about 8-bit groups. While most current computers work with bytes that are also 8 bits in size, that's not necessarily the case. In fact, "byte" is rather poorly defined, with considerable disagreement over what it means for sure -- so it's generally avoided when precision is needed.

Nonetheless, on a typical computer, an octet is going to be the same thing as a byte, and an octet stream will be stored in a series of bytes.

Jerry Coffin
A: 

An octet is 8 bits meant to be handled together (hence the "oct" in "octet"). It's what we think of when we say "byte" these days.

A char is basically a byte -- it's defined as the smallest addressable unit of memory, which on almost all modern computers is the same as an octet. But there have been computers with 9-bit, 16-bit, even 36-bit "words" that qualify as chars by that definition. You only need to care about those computers (and thus, about the difference between a char and an octet) if you have one -- let the people who have the weird hardware worry about how to make their programs run on it.

cHao
This doesn't only happen in ancient PDP's. Some DSP's from TI have 16 or 32-bit chars,
ninjalj
A: 

A string used to be a set of octets, which is turn is a set of 8 bits.

A string in C, is always a null-terminated, memory contiguous, set of bytes. Back in the day, each byte, an octet, represented a character. That's why they named the type used to make strings, char.

The ASCII table, that goes from 0 to 127, with the graphics/accents version going from 0 to 255, was no longer enough for displaying characters in a string, so someone though of adding bits to a character representation. Dumb-asses from CS though of 9bit and so forth, to what HW guys replied "are you nuts??? keep it a multiple of memory addressing unit", which was the byte, back then. Enter wide-character strings, i.e. 16bits per character. On a WC string, each character is represented by 2 bytes... there goes your char=1 byte rule down the drain.

To keep an exact description of a string, if it's a set of characters-represented-by-8bits (in Earth, following the ASCII table, but I've been to Mars), it's an "octet string".

If it's not "octet string" it may or may not be WC... Joel was a nice post on this.

jpinto3912