8

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kiran
  • 81
  • 1
  • 1
  • 2
  • 1
    Sorry, only those who have taken lessons playing the Octet can write programs on Octet string. ;-) They are stored in memory along with other variables. I would only worry about Octets when the one of the other musicians doesn't show. ;-) – Thomas Matthews Jul 08 '10 at 19:35

5 Answers5

15

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
  • 476,176
  • 80
  • 629
  • 1,111
1
  • 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
  • 74,049
  • 16
  • 131
  • 175
  • 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 Jul 08 '10 at 19:36
  • 3
    @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 Jul 08 '10 at 19:43
  • @jp: http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char – kennytm Jul 08 '10 at 19:52
  • @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 Jul 08 '10 at 19:55
  • @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 Jul 08 '10 at 20:05
  • @jp: All I know is ISO C requires `CHAR_BIT ≥ 8`. I don't know how it is in ANSI C. – kennytm Jul 08 '10 at 20:08
  • @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 Jul 08 '10 at 20:37
  • @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 Jul 13 '10 at 10:41
  • 1
    You can (or, more likely, could once upon a time) have 9-bit `char` on machines with 36-bit word size. There were also other words lengths that were not a multiple of 8 bits. – Jonathan Leffler Jun 24 '13 at 11:50
1

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
  • 84,970
  • 20
  • 145
  • 172
0

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 H
  • 7,311
  • 5
  • 45
  • 54
-1

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
  • 1,457
  • 2
  • 12
  • 19
  • An octet is an 8-bit byte. It is in contrast to other byte sizes that sometimes surfaced, like the 6-bit, 9-bit or 12-bit bytes on old hardware like PDP's. – jww Sep 24 '17 at 05:07