Re: String to Octal



fuch6921@xxxxxxxxxxxxx wrote:
I want to read in an Octal number argument and have it stored as an
octal number. For instance the user will type: ./a.out 777 and it
will store the octal number 777. But it atoi does this as an interger,
and sscanf gives me 0.


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

int main(void)
{
char *src[] = { "309", "511", "777", "1ff", "1411" }, *endp;
size_t i, n = sizeof src / sizeof *src;
unsigned long x;
printf(" Note: when * endp = '\\0', it is printed as '$'\n\n");
for (i = 0; i < n; i++) {
printf(" The input string is \"%s\"\n", src[i]);
x = strtoul(src[i], &endp, 8);
printf("read as octal, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n",
*endp ? *endp : '$', x, x, x);
x = strtoul(src[i], &endp, 10);
printf("read as decimal, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n",
*endp ? *endp : '$', x, x, x);
x = strtoul(src[i], &endp, 16);
printf("read as hex, *endp='%c',\n"
" value: %#lo (oct), %lu (dec), %#lx (hex)\n\n",
*endp ? *endp : '$', x, x, x);
}
return 0;
}

Note: when * endp = '\0', it is printed as '$'

The input string is "309"
read as octal, *endp='9',
value: 030 (oct), 24 (dec), 0x18 (hex)
read as decimal, *endp='$',
value: 0465 (oct), 309 (dec), 0x135 (hex)
read as hex, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)

The input string is "511"
read as octal, *endp='$',
value: 0511 (oct), 329 (dec), 0x149 (hex)
read as decimal, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)
read as hex, *endp='$',
value: 02421 (oct), 1297 (dec), 0x511 (hex)

The input string is "777"
read as octal, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)
read as decimal, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)
read as hex, *endp='$',
value: 03567 (oct), 1911 (dec), 0x777 (hex)

The input string is "1ff"
read as octal, *endp='f',
value: 01 (oct), 1 (dec), 0x1 (hex)
read as decimal, *endp='f',
value: 01 (oct), 1 (dec), 0x1 (hex)
read as hex, *endp='$',
value: 0777 (oct), 511 (dec), 0x1ff (hex)

The input string is "1411"
read as octal, *endp='$',
value: 01411 (oct), 777 (dec), 0x309 (hex)
read as decimal, *endp='$',
value: 02603 (oct), 1411 (dec), 0x583 (hex)
read as hex, *endp='$',
value: 012021 (oct), 5137 (dec), 0x1411 (hex)

.



Relevant Pages

  • Re: Saving double data as 16 bit tiff fle
    ... TIFF files cannot store floating point information, ... load the TIFF, pull out the R, G, and B channels, convert them to hex, slam the 6 bytes of hex together, and then use ... and data classes and value ...
    (comp.soft-sys.matlab)
  • Re: what should atoi() do with garbage input?
    ... > appropriate place in the Standard), what atoi() ought ... they decompose the input string into three ... white-space characters (as specified by the isspace ... a subject sequence resembling an integer ...
    (comp.lang.c)
  • Re: ecb works , but cfb and cbc dont ( php )
    ... I just convert the binary to hex and store it all in a ... i can then ftp, email this file anywhere and decrypt it ... stuff right takes a considerable amount of knowledge. ...
    (sci.crypt)
  • Visual Basic Hex conversion
    ... I am getting hex numbers in from an RS-232 source and coverting them to store ... in a database. ...
    (microsoft.public.vb.general.discussion)
  • Re: Does input field can store a thousand characters?
    ... field needs to store at least a thousand characters? ... then you may run into a limitation on the maximum length of the input string. ...
    (comp.lang.javascript)