473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Print a signel octal

Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !
Dec 18 '07 #1
10 3548
On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !
%ho
Dec 18 '07 #2
Ali Karaali said:
On 18 Aral?k, 17:06, laurent.paul...@techway.fr wrote:
>With a signed short it's printf %hi but what about an octal ?
%ho
%it's %off %to %work %we %go...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Dec 18 '07 #3
Ali Karaali wrote:
On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
>Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !

%ho
That's the format specifier for an unsigned short to be formatted as an
octal, not for a signed int formatted as an octal.

The *printf() functions don't provide what the OP wants. If you want a
signed int printed as an octal you can do this:

printf("%c%o",octal<0 ? '-' : ' ', octal<0 ? -octal : octal);

...although this may fail for octal < -INT_MAX.

Phil
Dec 18 '07 #4
la*************@techway.fr wrote:
Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?
The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code like
the following. You might also want to think about what things might be
done better. Note that if you start with a signed int value, you should
probably memmove or memcpy it to an unsigned int used as the argument
for show_signed_octal (unless you _know_ your clever pointer trick is
legal and works).

#include <stdio.h>
#include <limits.h>

void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}

int main(void)
{
unsigned int x = 0, increment = UINT_MAX / 13, i;
show_signed_octal(0);
for (i = 1; i < 14; i++) {
x += increment;
show_signed_octal(x);
}
show_signed_octal((unsigned) INT_MIN);
show_signed_octal((unsigned) INT_MAX);
show_signed_octal(UINT_MAX);
return 0;
}
[Output for one implementation]
argument a = 0 is non-negative.

argument a = 02354235423 is non-negative.

argument a = 04730473046 is non-negative.

argument a = 07304730471 is non-negative.

argument a = 011661166114 is non-negative.

argument a = 014235423537 is non-negative.

argument a = 016611661162 is non-negative.

argument a = 021166116605 is negative,
1 + ~a = -016611661173,
1 + ~(1 + ~a) = 021166116605.

argument a = 023542354230 is negative,
1 + ~a = -014235423550,
1 + ~(1 + ~a) = 023542354230.

argument a = 026116611653 is negative,
1 + ~a = -011661166125,
1 + ~(1 + ~a) = 026116611653.

argument a = 030473047276 is negative,
1 + ~a = -07304730502,
1 + ~(1 + ~a) = 030473047276.

argument a = 033047304721 is negative,
1 + ~a = -04730473057,
1 + ~(1 + ~a) = 033047304721.

argument a = 035423542344 is negative,
1 + ~a = -02354235434,
1 + ~(1 + ~a) = 035423542344.

argument a = 037777777767 is negative,
1 + ~a = -011,
1 + ~(1 + ~a) = 037777777767.

argument a = 020000000000 is negative,
1 + ~a = -020000000000,
1 + ~(1 + ~a) = 020000000000.

argument a = 017777777777 is non-negative.

argument a = 037777777777 is negative,
1 + ~a = -01,
1 + ~(1 + ~a) = 037777777777.
Dec 18 '07 #5
Ali Karaali wrote:
On 18 Aralık, 17:06, laurent.paul...@techway.fr wrote:
>Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !

%ho
"%o" (and "%ho") are *defined* as taking unsigned values to be shown as
unsigned. Your "solution" in no way addresses the question of printing
"signed octal".
Dec 18 '07 #6
Philip Potter wrote:
Ali Karaali wrote:
>On 18 Aralęk, 17:06, laurent.paul...@techway.fr wrote:
>>Hello,

I would like to print a signed octal.
With a signed short it's printf %hi but what about an octal ?

Tk !
%ho

That's the format specifier for an unsigned short to be formatted as an
octal, not for a signed int formatted as an octal.

The *printf() functions don't provide what the OP wants. If you want a
signed int printed as an octal you can do this:

printf("%c%o",octal<0 ? '-' : ' ', octal<0 ? -octal : octal);
Argh! This should be:

short octal = f(); /* f() gets the value to be printed */
unsigned short value = (octal<0) ? -octal : octal;
printf("%c%o",octal<0 ? '-' : ' ', value);

...because before I was passing a signed value where printf() expected
unsigned.
..although this may fail for octal < -INT_MAX.

Phil
Dec 19 '07 #7
Martin Ambuhl wrote:

[...]
The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code like
the following. You might also want to think about what things might be
done better.

I would like to suggest an improvement too. :)

#include <stdio.h>
#include <limits.h>

void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}
For signed integer types, it's implementation-defined, if the object
representation with sign bit 1 and the value bits equal, is a trap
representation or not.

Not assuming 2's complement machine:
void print_octal(int signed_num)
{
if (signed_num < 0)
{
printf("-%#o\n", (unsigned int)(signed_num * (-1)) );
}
else
{
printf("%#o\n", (unsigned int)signed_num);
}
}

--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Dec 19 '07 #8
Tor Rustad wrote:
Martin Ambuhl wrote:

[...]
>The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code like
the following. You might also want to think about what things might be
done better.
>#include <stdio.h>
#include <limits.h>

void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}

For signed integer types, it's implementation-defined, if the object
representation with sign bit 1 and the value bits equal, is a trap
representation or not.
It's lucky that Martin didn't deal with signed integer types then.
Not assuming 2's complement machine:
Martin's didn't either, though I initially thought it did. Because it
takes an unsigned int argument, any negative numbers will first be
shifted into the range of unsigned int, when the 1 + ~a operation
correctly gets the absolute value of the original negative number:

For example:
show_signed_octal(-3);

With any signed integer representation, -3 when converted to unsigned
int becomes UINT_MAX+1-3, which will be represented as
....111101 (any number of leading ones)
after complement and add one the representation is
....000011 (any number of leading zeroes)
which has value 3. Hey presto!

Phil
Dec 20 '07 #9
Philip Potter wrote:
Tor Rustad wrote:
>Martin Ambuhl wrote:

[...]
>>The octal ("%o") and hex ("%x" or "%X") specifiers are for unsigned
values. If you truly want to print values as signed, consider code
like the following. You might also want to think about what things
might be done better.
>>#include <stdio.h>
#include <limits.h>

void show_signed_octal(unsigned int a)
{
if (a <= INT_MAX)
printf("argument a = %#o is non-negative.\n\n", a);
else {
printf("argument a = %#o is negative,\n ", a);
a = 1 + ~a;
printf(" 1 + ~a = -%#o, \n", a);
a = 1 + ~a;
printf(" 1 + ~(1 + ~a) = %#o.\n\n", a);
}
}

For signed integer types, it's implementation-defined, if the object
representation with sign bit 1 and the value bits equal, is a trap
representation or not.

It's lucky that Martin didn't deal with signed integer types then.
>Not assuming 2's complement machine:

Martin's didn't either, though I initially thought it did. Because it
takes an unsigned int argument, any negative numbers will first be
shifted into the range of unsigned int, when the 1 + ~a operation
correctly gets the absolute value of the original negative number:

For example:
show_signed_octal(-3);

With any signed integer representation, -3 when converted to unsigned
int becomes UINT_MAX+1-3, which will be represented as
...111101 (any number of leading ones)
after complement and add one the representation is
...000011 (any number of leading zeroes)
which has value 3. Hey presto!

Phil
/*
Signed Octal?
*/

#include <stdio.h>

int main(void) {
int a = 123;
int b = -123;
printf("%s%o\n", a < 0 ? "-" : "", a < 0 ? -a : a);
printf("%s%o\n", b < 0 ? "-" : "", b < 0 ? -b : b);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 21 '07 #10
Joe Wright wrote:
/*
Signed Octal?
*/

#include <stdio.h>

int main(void) {
int a = 123;
int b = -123;
printf("%s%o\n", a < 0 ? "-" : "", a < 0 ? -a : a);
printf("%s%o\n", b < 0 ? "-" : "", b < 0 ? -b : b);
return 0;
}
This code is almost identical to code I've already posted elsethread in
message <fk**********@aioe.org>. It even has the same bug!

Phil
Dec 22 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Sean McIlroy | last post by:
I recently found out that unicode("\347", "iso-8859-1") is the lowercase c-with-cedilla, so I set out to round up the unicode numbers of the extra characters you need for French, and I found them...
1
by: Jim | last post by:
I need to input a stream of data that is a string. In that string are some octal numbers. What happen, did you forget the ushort.Parse(string,...) that accepts octal numbers? You have decimal...
7
by: gk245 | last post by:
I have this: #include <stdio.h> int main (void) { int number; printf ("Enter number: ");
27
by: fuch6921 | last post by:
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...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
7
by: Gary Brown | last post by:
Hi, I have a whole bunch of integer constants that are best given in octal (PDP-1 opcodes). It makes a huge difference in readability and authenticity if these can be entered in octal. I...
14
by: mosi | last post by:
Problem: how to get binary from integer and vice versa? The simplest way I know is: a = 0100 a 64 but: a = 100 (I want binary number) does not work that way.
1
by: HaifaCarina | last post by:
Here's the code is used but...but still something is wrong... i need help... /*CONVERTING DECIMAL TO OCTAL*/ String inputDeci,octal=""; int deci,count=0, i, h; ...
25
by: notahipee | last post by:
I have been trying to cin an number from 0 to 9 with a leading 0. For example 00 or 07. I was using a switch case. switch (int) { case 01: break; case 02: break;..... My problem arises at 08...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.