octal

What are the Java semantics of an escaped number in a character literal, e.g. '\\15' ?

删除回忆录丶 提交于 2019-11-28 16:27:33
Please explain what, exactly, happens when the following sections of code are executed: int a='\15'; System.out.println(a); this prints out 13; int a='\25'; System.out.println(a); this prints out 21; int a='\100'; System.out.println(a); this prints out 64. Bohemian You have assigned a character literal, which is delimited by single quotes, eg 'a' (as distinct from a String literal, which is delimited by double quotes, eg "a" ) to an int variable. Java does an automatic widening cast from the 16-bit unsigned char to the 32-bit signed int . However, when a character literal is a backslash

Understanding str_pad() with leading zeros

爷,独闯天下 提交于 2019-11-28 14:23:44
I'm trying to build a php function and discovered some weird behavior and I can't even formulate a proper question, so if anyone can explain what is going on, I would appreciate it. I'm working with a set of numbers with leading zeros, and its important that they be maintained, but users almost never input leading zeros. So I use this: $x = 123; $n = 5; $x = str_pad((int)$x,$n,"0",STR_PAD_LEFT); echo $x; and, as desired, this gets me 00123. The weird stuff happens when I tested for a user inputting a zero before their number $x = 0123; $n = 5; $x = str_pad((int)$x,$n,"0",STR_PAD_LEFT); echo $x

JavaScript - Preventing octal conversion

眉间皱痕 提交于 2019-11-28 14:18:45
I'm taking a numerical input as an argument and was just trying to account for leading zeroes. But it seems javascript converts the number into octal before I can do anything to the number. The only way to work around it so far is if I pass the number as a string initially but I was hoping there'd be another way to convert it after it is passed? So far tried (using 017 which alerted me to the octal behaviour): 017.toString(10) // 15 parseInt(017,10) // 15 017 + "" //15 new Number(017) //15 new Number('017') //17 parseInt('017', 10) // 17 So given function(numb) { if (typeof numb === number) {

Why output of int 070 is 56 in C program? [duplicate]

孤街浪徒 提交于 2019-11-28 13:55:39
问题 This question already has answers here : What is special about numbers starting with zero? (4 answers) Closed 6 months ago . Can you explain it? Why it given 56 value as output? #include <stdio.h> #include <conio.h> void main() { int x = 070; printf("%d", x); getch(); } 回答1: Any integer literal (integer constant) starting with 0 is an octal representation. Quoting C11 , chapter §6.4.4.1, Integer constants octal-constant:  0   octal-constant octal-digit and octal-digit : one of 0 1 2 3 4 5 6 7

How to work with leading zeros in integers

自古美人都是妖i 提交于 2019-11-28 12:14:16
What is the proper way to deal with leading zeros in Ruby? 0112.to_s => "74" 0112.to_i => 74 Why is it converting 0112 into 74 ? How can convert 0112 to a string "0112" ? I want to define a method that takes integer as a argument and returns it with its digits in descending order. But this does not seem to work for me when I have leading zeros: def descending_order(n) n.to_s.reverse.to_i end falsetru A numeric literal that starts with 0 is an octal representation, except the literals that start with 0x which represent hexadecimal numbers or 0b which represent binary numbers. 1 * 8**2 + 1 * 8*

long value with 0 on left

流过昼夜 提交于 2019-11-28 10:02:12
问题 Why this behavior happens? long value = 123450; System.out.println("value: " + value); value: 123450 long value = 0123450; // ^ System.out.println("value: " + value); value: 42792 What is this 42792? 回答1: Why this behavior happens? Just as literals starting with 0x are treated as hexadecimal numbers (base 16), literals starting with a 0 are treated as octal numbers, i.e., numbers in base 8. (Try writing 0789, and you'll see that the compiler will complain.) What is this 42792? The number

Decimal Conversion error

社会主义新天地 提交于 2019-11-28 09:04:10
问题 I am writing a program that will convert octal numbers to decimals. It compiles right and everything but there is something majorily wrong with my conversion code. It seems perfectly logic to me, however somehow when I run the program the conversions are wrong (i.e. 1 is converted to 36) can someone point out what is going wrong? public static int convert(int octal) { int d1=0,d2=0,d3=0,d4=0,d5=0,d6=0,d7=0,d8=0; if(octal >=9999999){ d8 = (octal-(octal%10000000));} if(octal >=999999){ d7 =

Invalid Token when using Octal numbers

笑着哭i 提交于 2019-11-28 07:55:15
I'm a beginner in python and I'm trying to use a octal number in my script, but when I try it, it returns me that error: >>> a = 010 SyntaxError: invalid token (<pyshell#0>, line 1) >>> 01 SyntaxError: invalid token (<pyshell#1>, line 1) There's something wrong with my code? I'm using Python3 (and reading a python 2.2 book) Try 0o10 , may be because of python 3, or pyshell itself. PEP says, octal literals must now be specified with a leading "0o" or "0O" instead of "0"; http://www.python.org/dev/peps/pep-3127/ 来源: https://stackoverflow.com/questions/1837874/invalid-token-when-using-octal

Octal representation inside a string in C

半腔热情 提交于 2019-11-28 05:02:48
问题 In the given program: int main() { char *p = "\0777"; printf("%d %d %d\n",p[0],p[1],p[2]); printf("--%c-- --%c-- --%c--\n",p[0],p[1],p[2]); return 0; } It is showing the output as: 63 55 0 --?-- --7-- ---- I can understand that it is converting the first two characters after \0 (\077) from octal to decimal but can any one explain me why 2 characters, why not 1 or 3 or any other ? Please explain the logic behind this. 回答1: char *p = "\07777"; Here a string literal assigned to a pointer to a

Why are Octal numeric literals not allowed in strict mode (and what is the workaround?)

家住魔仙堡 提交于 2019-11-28 01:50:47
Why are Octal numeric literals not allowed in JavaScript strict mode ? What is the harm? "use strict"; var x = 010; //Uncaught SyntaxError: Octal literals are not allowed in strict mode. <h1>Check browser console for errors</h1> In case a developer needs to use Octals (which can mistakenly change a numbers meaning ), is there a workaround? The "why" part of the question is not really answerable. As for "how", off the top of my head... "use strict"; var x = parseInt('010', 8); document.write(x); Octal literals are not allowed because disallowing them discourages programmers from using leading