Converting Decimal to Hexadecimal and Octal - c

Show how to write a constant in C, whose decimal value is 65 as
a. a hexadecimal constant
65/16 = 1 r1
1/16 = 0 r1
Hexadecimal constant = 11
b. an octal constant (in C)
65/8 = 8 r1
8/8 = 1 r0
1/8 = 0 r1
Octal constant = 101
Is this the right way to convert constants in C?

You just need a while loop and a string. As this is homework, I do not think I should say more than that.

The method is to divide by the base until the result is less than the base.
So 65/8 gives 8 r1 but you don't stop there because the result is 8 not less than 8
You divide by 8 again and get 1
It should be
65/64 = 10 r 1 where 64 = 8x8 = octal 10
I don't think I've said too much

Maybe I am misunderstanding the questions, but it seems like you are being asked how hex and oct constants are represented in C, not how to implement an algorithm to convert dec to hex and oct.
If that is the case:
hex numbers are represented by a preceding 0x or 0X
oct numbers are represented by a preceding 0
int hex = 0x41;
int oct = 0101;
Of course, you can verify this by printing our the values in decimal:
printf("%d\n", hex);
printf("%d\n", oct);

Related

Meaning of leading zero in integer literal

I'm studying (ANSI) C by The C Programming Language (2nd Edition).
This is a code snippet from 2.10 Assignment Operators and Expressions:
1 /* bitcount() counts the number of 1-bits in its integer argument */
2 int bitcount(unsigned x)
3 {
4 int b;
5 for (b = 0; x != 0; x >>= 1)
6 if (x & 01)
7 b++;
8 return b;
9 }
I am confused why x & 01 is written in line 6 rather than x & 1 or x & 0x1? Is 0 before 1 necessary?
The 01 could also be written 1, or 0x1.
01 = Octal Constant Base 8 (1 Decimal)
1 = Decimal Constant. Base 10
0x1 = Hexadecimal Constant. Base 16 (1 Decimal).
When the book was written, Base 8 (octal) was pervasive in existing computer programming.
A leading 0 makes a constant into an octal (base 8) value.
In this case it's no different from 1 or 0x1 because they will all have the same binary representation of 000...001. People often use hex constants to distinguish that a value is being used as a bitmask or other bitwise value today. In the past, octal constants were more often used for the same purpose.
0x1 = hex constant = 1
0X1 = hex constant = 1
01 = octal contant = 1
1 = decimal constant = 1
1U = unsigned decimal constant = 1
Indeed "01" is distinct from 1 and 0x1 in principle. "01" is encoded in octal. Kernighan/Ritchie probably value the ease in which one can convert from octal to binary for this particular problem.

How is this bitwise AND operator masking the lower seven order bits of the number?

I am reading The C Programming Language by Brian Kernigan and Dennis Ritchie. Here is what it says about the bitwise AND operator:
The bitwise AND operator & is often used to mask off some set of bits, for example,
n = n & 0177
sets to zero all but the low order 7 bits of n.
I don't quite see how it is masking the lower seven order bits of n. Please can somebody clarify?
The number 0177 is an octal number representing the binary pattern below:
0000000001111111
When you AND it using the bitwise operation &, the result keeps the bits of the original only in the bits that are set to 1 in the "mask"; all other bits become zero. This is because "AND" follows this rule:
X & 0 -> 0 for any value of X
X & 1 -> X for any value of X
For example, if you AND 0177 and 0545454, you get
0000000001111111 -- 0000177
0101010101010101 -- 0545454
---------------- -------
0000000001010101 -- 0000154
In C an integer literal prefixed with 0 is an octal number so 0177 is an octal number.
Each octal digit (of value 0 to 7) is represented with 3 bits and 7 is the greatest value for each digit. So a value of 7 in octal means 3 bits set.
Since 0177 is an octal literal and each octal number is 3 three bits you have, the following binary equivalents:
7 = 111
1 = 001
Which means 0177 is 001111111 in binary.
It is already explained that the first '0' used for octal representation of a number in ANSI C. Actually, the number 0177 (octal) is same with 127 (in decimal), which is 128-1 and also can be represented as 2^7-1, and 2^n-1 in binary representation means take n 1's and put all the 1's to the right.
0177 = 127 = 128-1
which is a bitmask;
0000000000000000000000001111111
You can check the code down below;
Demo
#include <stdio.h>
int main()
{
int n = 0177; // octal representation of 127
printf("Decimal:[%d] : Octal:[%o]\n", n, n, n);
n = 127; // decimal representation of 127
printf("Decimal:[%d] : Octal:[%o]\n", n, n, n);
return 0;
}
Output
Decimal:[127] : Octal:[177]
Decimal:[127] : Octal:[177]
0177 is an octal value each digit is represented by 3 bits form the value 000 to 111 so 0177 translates to 001111111 (i.e 001|111|111) which if you consider in 32 bit binary ( can be 64 bit too except the remainder of the digits are populated as per the MSB i.e sign bit in this case value 0) form is 0000000000000000000000001111111 and and performing a bitwise with it for a given number, will output the lower 7 bits of the number turning of rest of the digits in the n-bit number to 0.
(since x&0 =0 & x&1=x e.g 0&0=0 ,1&0=0, 1&1=1 0&1=1)

Hex to Octal Conversion Program Without Using Decimal or Binary

Today I was just playing around for basic conversions from one base to another. I goggled some code for converting from hex to octal, and I noticed that it mostly uses intermediate conversion to either decimal or binary and then back to octal.Is it possible write my own function for converting hex string to octal string without using any intermediate conversion.Also I do not want to use inbuilt printf option like %x or %o. Thanks for your inputs.
Of course it is possible. A number is a number no matter what numeric system it is in. The only problem is that people are used to decimal and that is why they understand it better. You may convert from any base to any other.
EDIT: more info on how to perform the conversion.
First note that 3 hexadecimal digits map to exactly 4 octal digits. So having the number of hexadecimal digits you may find the number of octal digits easily:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int get_val(char hex_digit) {
if (hex_digit >= '0' && hex_digit <= '9') {
return hex_digit - '0';
} else {
return hex_digit - 'A' + 10;
}
}
void convert_to_oct(const char* hex, char** res) {
int hex_len = strlen(hex);
int oct_len = (hex_len/3) * 4;
int i;
// One hex digit left that is 4 bits or 2 oct digits.
if (hex_len%3 == 1) {
oct_len += 2;
} else if (hex_len%3 == 2) { // 2 hex digits map to 3 oct digits
oct_len += 3;
}
(*res) = malloc((oct_len+1) * sizeof(char));
(*res)[oct_len] = 0; // don't forget the terminating char.
int oct_index = oct_len - 1; // position we are changing in the oct representation.
for (i = hex_len - 1; i - 3 >= 0; i -= 3) {
(*res)[oct_index] = get_val(hex[i]) % 8 + '0';
(*res)[oct_index - 1] = (get_val(hex[i])/8+ (get_val(hex[i-1])%4) * 2) + '0';
(*res)[oct_index - 2] = get_val(hex[i-1])/4 + (get_val(hex[i-2])%2)*4 + '0';
(*res)[oct_index - 3] = get_val(hex[i-2])/2 + '0';
oct_index -= 4;
}
// if hex_len is not divisible by 4 we have to take care of the extra digits:
if (hex_len%3 == 1) {
(*res)[oct_index] = get_val(hex[0])%8 + '0';
(*res)[oct_index - 1] = get_val(hex[0])/8 + '0';
} else if (hex_len%3 == 2) {
(*res)[oct_index] = get_val(hex[1])%8 + '0';
(*res)[oct_index - 1] = get_val(hex[1])/8 + (get_val(hex[0])%4)*4 + '0';
(*res)[oct_index - 2] = get_val(hex[0])/4 + '0';
}
}
Also here is the example on ideone so that you can play with it: example.
It's a little tricky as you will be converting groups of 4 bits to groups of 3 bits - you'll probably want to work with 12 bits at a time, i.e. 3 hex digits to 4 octal digits and you'll then have to deal with any remaining bits separately.
E.g. to convert 5274 octal to hex:
5 2 7 4
101 010 111 100
|||/ \\// \|||
1010 1011 1100
A B C
All numbers in computer's memory are base 2. So whenever you want to actually DO something with the values (mathematical operations), you'll need them as ints, floats, etc. So it's handy or may come handy in the future to do the conversion via computable types.
I'd avoid direct string to string conversions, unless the values can be too big to fit into a numeric variable. It is surprisingly hard to write reliable converter from scratch.
(Using base 10 makes very little sense in a binary computer.)
Yes, you can do it relatively easily: four octal digits always convert to three hex digits, so you can split your string into groups of three hex digits, and process each group from the back. If you do not have enough hex digits to complete a group of three, add leading zeros.
Each hex digit gives you four bits; take the last three, and convert them to octal. Add the next four, and take three more bits to octal. Add the last group of four - now you have six bits in total, so convert them to two octal digits.
This avoids converting the entire number to a binary, although there will be a "sliding" binary window used in the process of converting the number.
Consider an example: converting 62ABC to octal. Divide into groups of three digits: 062 and ABC (note the added zero in front of 62 to make a group of three digits).
Start from the back:
C, or 1100, gets chopped into 1 and 100, making octal 4, and 1 extra bit for the next step
B, or 1011, gets chopped into 10 for the next step and 11 for this step. The 1 from the previous step is attached on the right of 11, making an octal 7
A, or 1010, gets chopped into 101 and 0. The 10 from the previous step is attached on the right, making 010, or octal 2. The 101 is octal 5, so we have 5274 so far.
2 becomes 2 and 0 for the next step;
6 becomes 4 and 01 for the next step;
0 becomes 0 and 1 (because 01 from the previous step is added).
The final result is 01425274.
Seems like a pretty straight forward task to me... You want a hex string and you want to convert it to an octal string. Let's take the ASCII hex and convert it to an int type to work with:
char hex_value[] = "0x123";
int value = strtol(hex_value,NULL,16);
It's still hex at this point, then if we want to convert from one base to another there's simple math that can be done:
123 / 8 = 24 R 3
24 / 8 = 4 R 4
4 / 8 = 0 R 4
This tells us that 12316 == 4438 so all we have to do is write that math into a basic function and put the final value back into a string:
char * convert_to_oct(int hex)
{
int ret = 0, quotient = 0, reminder = 0, dividend = hex, counter = 0, i;
char * ret_str; // returned string
while(dividend > 0){ // while we have something to divide
quotient = dividend / 0x8; // get the quotient
reminder = dividend - quotient * 0x8; // get the reminder
ret += reminder * pow(10, counter); // add the reminder (shifted)
// into our return value
counter++; // increment our shift
dividend = quotient; // get ready for the next divide operation
}
ret_str = malloc(counter); // allocate the right number of characters
sprintf(ret_str, "%d", ret); // store the result
return ret_str;
}
So this function will convert a hex (int) value into a oct string. You could call it like:
int main()
{
char hex_value[] = "0x123";
char * oct_value;
int value = strtol(hex_value,NULL,16);
// sanity check, see what the value should be before the convert
printf("value is %x, auto convert via printf gives %o\n", value, value);
oct_value = convert_to_oct(value);
printf("value is %s\n", oct_value);
All octal digits contain 3 bits of information. All Hex digits contain 12 bits of information. The least common multiple of 3 and 4 is 12.
This means you can build a simple lookup table
0000 = 0x000
0001 = 0x001
0002 = 0x002
...
0007 = 0x007
0010 = 0x008
0011 = 0x009
0012 = 0x00A
...
0017 = 0x00F
0020 = 0x010
...
5274 = 0xABC
...
Now that the idea is there, you have several choices:
Build a Map (lookup table)
The routine here would add leading zeros to the octal (string) number until it was 4 digits long, and then lookup the hexadecimal value from the table. Two variations are typing out the table statically, or populating it dynamically.
Use math to replace the lookup table
Instead of typing out each solution, you could calculate them
hexdigit1 = 01 & octaldigit8 + octaltdigit1;
hexdigit16 = 03 & octaldigit64 << 02 + 06 & octaldigit8 >> 01;
hexdigit256 = octaldigit512 << 01 + 01 & octaldigit64;
where the octaldigit1 / hexdigit16 / octaldigit8 means "octal 1's place", "hexadecimal 16's place", "octal 8's place" respectively.
Note that in either of these cases you don't "use binary" or "use decimal" but as these numbers can be represented in either of those two systems, it's not possible to avoid someone coming along behind and analyzing the correctness of the (or any) solution in decimal or binary terms.
Here is an easy function to convert your characters into javascript. valid for ALERT or for your pages up to 65536 32BITS. The concern you encounter is often for the text for codes beyond 127. The safest value is the OCTAL. ParseXX to avoid.
Thank you for your likes (^ _ ^). it's free to enjoy.
function enjoyOCTALJS (valuestr){
var arrstr = valuestr.split('');
arrstr = arrstr.map(f => (!isNaN(f.charCodeAt(0)))? (f.charCodeAt(0)>127)? '\\'+f.charCodeAt(0).toString(8):f:f);
return arrstr.join('');
}
If you just want to get the octal value of a character do this: Max = 65536 ou 0XFFFF.
var mchar = "à";
var result = mchar.charCodeAt(0).toString(8);
Or completely :
var mchar = 'à';
var result = mchar.codePointAt(0).toString(8);
If value > 65536 return UNDEFINED. You can use the function parameter to increase the value. charCodeAt(x) or codePointAt(x).
Your computer considers everything as 0 to 255.
We do not need to do big functions to convert characters, it's very easy.
CHAR TO UNICIDE
var mchar = 'à';
var result = mchar.codePointAt(0); or mchar.charCodeAt();
UNICODE TO OCTAL :
var mcode = 220;
var result = mcode.toString(8);
etc... :)

When does the signedness of an integer really matter?

Due to the way conversions and operations are defined in C, it seems to rarely matter whether you use a signed or an unsigned variable:
uint8_t u; int8_t i;
u = -3; i = -3;
u *= 2; i *= 2;
u += 15; i += 15;
u >>= 2; i >>= 2;
printf("%u",u); // -> 2
printf("%u",i); // -> 2
So, is there a set of rules to tell under which conditions the signedness of a variable really makes a difference?
It matters in these contexts:
division and modulo: -2/2 = 1, -2u/2 = UINT_MAX/2-1, -3%4 = -3, -3u%4 = 1
shifts. For negative signed values, the result of >> and << are implementation defined or undefined, resp. For unsigned values, they are always defined.
relationals -2 < 0, -2u > 0
overflows. x+1 > x may be assumed by the compiler to be always true iff x has signed type.
Yes. Signedness will affect the result of Greater Than and Less Than operators in C. Consider the following code:
unsigned int a = -5;
unsigned int b = 7;
if (a < b)
printf("Less");
else
printf("More");
In this example, "More" is incorrectly output, because the -5 is converted to a very high positive number by the compiler.
This will also affect your arithmetic with different sized variables. Again, consider this example:
unsigned char a = -5;
signed short b = 12;
printf("%d", a+b);
The returned result is 263, not the expected 7. This is because -5 is actually treated as 251 by the compiler. Overflow makes your operations work correctly for same-sized variables, but when expanding, the compiler does not expand the sign bit for unsigned variables, so it treats them as their original positive representation in the larger sized space. Study how two's compliment works and you'll see where this result comes from.
It affects the range of values that you can store in the variable.
It is relevant mainly in comparison.
printf("%d", (u-3) < 0); // -> 0
printf("%d", (i-3) < 0); // -> 1
Overflow on unsigned integers just wraps around. On signed values this is undefined behavior, everything can happen.
The signedness of 2's complement numbers is simply just a matter of how you are interpreting the number. Imagine the 3 bit numbers:
000
001
010
011
100
101
110
111
If you think of 000 as zero and the numbers as they are natural to humans, you would interpret them like this:
000: 0
001: 1
010: 2
011: 3
100: 4
101: 5
110: 6
111: 7
This is called "unsigned integer". You see everything as a number bigger than/equal to zero.
Now, what if you want to have some numbers as negative? Well, 2's complement comes to rescue. 2's complement is known to most people as just a formula, but in truth it's just congruency modulo 2^n where n is the number of bits in your number.
Let me give you a few examples of congruency:
2 = 5 = 8 = -1 = -4 module 3
-2 = 6 = 14 module 8
Now, just for convenience, let's say you decide to have the left most bit of a number as its sign. So you want to have:
000: 0
001: positive
010: positive
011: positive
100: negative
101: negative
110: negative
111: negative
Viewing your numbers congruent modulo 2^3 (= 8), you know that:
4 = -4
5 = -3
6 = -2
7 = -1
Therefore, you view your numbers as:
000: 0
001: 1
010: 2
011: 3
100: -4
101: -3
110: -2
111: -1
As you can see, the actual bits for -3 and 5 (for example) are the same (if the number has 3 bits). Therefore, writing x = -3 or x = 5 gives you the same result.
Interpreting numbers congruent modulo 2^n has other benefits. If you sum 2 numbers, one negative and one positive, it could happen on paper that you have a carry that would be thrown away, yet the result is still correct. Why? That carry was a 2^n which is congruent to 0 modulo 2^n! Isn't that convenient?
Overflow is also another case of congruency. In our example, if you sum two unsigned numbers 5 and 6, you get 3, which is actually 11.
So, why do you use signed and unsigned? For the CPU there is actually very little difference. For you however:
If the number has n bits, the unsigned represents numbers from 0 to 2^n-1
If the number has n bits, the signed represents numbers from -2^(n-1) to 2^(n-1)-1
So, for example if you assign -1 to a an unsigned number, it's the same as assigning 2^n-1 to it.
As per your example, that's exactly what you are doing. you are assigning -3 to a uint8_t, which is illegal, but as far as the CPU is concerned you are assigning 253 to it. Then all the rest of the operations are the same for both types and you end up getting the same result.
There is however a point that your example misses. operator >> on signed number extends the sign when shifting. Since the result of both of your operations is 9 before shifting you don't notice this. If you didn't have the +15, you would have -6 in i and 250 in u which then >> 2 would result in -2 in i (if printed with %u, 254) and 62 in u. (See Peter Cordes' comment below for a few technicalities)
To understand this better, take this example:
(signed)101011 (-21) >> 3 ----> 111101 (-3)
(unsigned)101011 ( 43) >> 3 ----> 000101 ( 5)
If you notice, floor(-21/8) is actually -3 and floor(43/8) is 5. However, -3 and 5 are not equal (and are not congruent modulo 64 (64 because there are 6 bits))

What does 0xFE mean in a C program?

In the given program below,
void main()
{
int x=0xFE;
int y=0xF3;
.....
}
What values are assigned by x=0xFE and y=0xF3? What does 0x represent?
Writing a value with 0x before means it is written in hexadecimal notation, where the numbers 0-9 and additional "number" A-F are used to get a number system with the base 16. The big advantage of this is that each hexadecimal digit represents exactly 4 bits.
0xFE = 254
0xF3 = 243
So x = 254 and y = 243.
Numbers can be represented in different ways. In C, you can use decimal (e.g. 243), octal (e.g. 0363) or hexadecimal (0xF3).
If you write 243, you mean
243 = 2*10^2 + 4*10^1 + 3*10^0 = 2*100 + 4*10 + 3 = 243
where ^ means "to the power of". That means our normal numbers are base 10, or decimal. Hexadecimal uses base 16, and the digits are 0123456789ABCDEF, where 0=0, ... 9=9, A=10, B=11, ... F=15. So 243 can also be written as
0xF3 = 15*16^1 + 3*16^0 = 15*16 + 3 = 243
That is what you see. In other words, 0xF3 is just another way to write 243, and 0xFE is another way to write 254 (15*16 + 14 = 254).
The advantage is that each hexadecimal digit represents 4 bits, so 2 hexadecimal digits can be used to display a byte. If you know that 0x0 is 0000 in binary, and so on, up to 0xF, which is 1111 in binary, an experienced user can easily "see" the bits in a byte.
FWIW, octal is in base 8, so the only digits are 01234567. I have always found it a bit awkward to use.
The values are in hexadecimal since they are preceded by the 0x identifier.
To convert the values, you can use your computer calculator on programmer mode or google.
http://screensnapr.com/v/aQEPYk.png

Resources