literals

What happens when a char array gets initialized from a string literal?

梦想的初衷 提交于 2020-02-03 08:15:49
问题 As I understand it, the following code works like so: char* cptr = "Hello World"; "Hello World" lives in the .rodata section of the program's memory. The string literal "Hello World" returns a pointer to the base address of the string, or the address of the first element in the so-called "array", since the chars are laid out sequentially in memory it would be the 'H'. This is my little diagram as I visualize the string literal getting stored in the memory: 0x4 : 'H' 0x5 : 'e' 0x6 : 'l' 0x6 :

Java: How to use byte literals greater than 0x7F

不问归期 提交于 2020-01-24 04:07:45
问题 In Java, I can't take a byte array of unsigned bytes (from something such as Wire Shark) and put this into java.... Because I will get compile errors since anything greater than 127 decimal/0x07F is treated not as a byte, but as an int.... IE: byte[] protocol = { 0x04, 0x01, 0x00, 0x50, /*error*/0xc1, /*error*/0xdb, 0x1c, /*error*/0x8c, 0x4d, 0x4f, 0x5a, 0x00 }; Need a good way to handle taking unsigned char arrays and putting them into Java as literals. 回答1: Cast them to (byte). 来源: https:/

Can I write a literal initializer in Java for a non-primitive type?

橙三吉。 提交于 2020-01-21 19:15:27
问题 Java's literal initializers are mostly used for primitives (e.g. int i = 2 and double j = 4.2 ), but there are certain non-primitive classes that also have literal initializers (e.g. String a = "Hello" or Number z = 43 ). I am looking to implement an initializer similar to that in a class that I wrote. I have a class Numeric that extends Number , that I would like to be able to initialize as Numeric a = 43 . Is there any way that this can be done in Java, and if so, how? Here is the part of

The difference between 0 and '0' in array

拜拜、爱过 提交于 2020-01-20 07:17:44
问题 I have a question about array initialization What is the difference between char a[6]={0}; and char a[6]={'0','0','0','0','0','0'}; How does the compiler interpret the above two expression? Are they just the same or not?? 回答1: '0' is the ASCII character for the number 0. Its value is 48. The constant 0 is a zero byte or null byte , also written '\0' . These four are equivalent: char a[6] = {0}; char a[6] = {0, 0, 0, 0, 0, 0}; char a[6] = {'\0', '\0', '\0', '\0', '\0', '\0'}; char a[6] = "\0\0

Why can I compare a String to a &str using if, but not when using match?

。_饼干妹妹 提交于 2020-01-13 08:33:11
问题 I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { let s = String::from("holla!"); if s == "holla!" { println!("it worked!"); } } But using a match statement (which I guess would be more elegant): fn main() { let s = String::from("holla!"); match s { "holla!" => println!("it worked!"), _ => println!("nothing"), } } I keep getting an error from the

Why can I compare a String to a &str using if, but not when using match?

我怕爱的太早我们不能终老 提交于 2020-01-13 08:32:29
问题 I'm trying to implement a function that reads command line arguments and compares them to hard-coded string literals. When I do the comparison with an if statement it works like a charm: fn main() { let s = String::from("holla!"); if s == "holla!" { println!("it worked!"); } } But using a match statement (which I guess would be more elegant): fn main() { let s = String::from("holla!"); match s { "holla!" => println!("it worked!"), _ => println!("nothing"), } } I keep getting an error from the

What's the advantage of having multi-line & single-line string literals in python?

隐身守侯 提交于 2020-01-11 10:24:27
问题 I know the triple quote strings are used as docstrings, but is there a real need to have two string literals? Are there any use case when identifying between single-line & multi-line is useful. in Clojure we have 1 string literal, is multi-line and we use it as docstring. So why the difference in python? 回答1: The advantage of having to be explicit about creating a multi-line string literal is probably best demonstrated with an example: with open("filename.ext) as f: for line in f: print(line

Unicode (hexadecimal) character literals in MySQL

∥☆過路亽.° 提交于 2020-01-11 02:46:22
问题 Is there a way to specify Unicode character literals in MySQL? I want to replace a Unicode character with an Ascii character, something like the following: Update MyTbl Set MyFld = Replace(MyFld, "ẏ", "y") But I'm using even more obscure characters which are not available in most fonts, so I want to be able to use Unicode character literals, something like Update MyTbl Set MyFld = Replace(MyFld, "\u1e8f", "y") This SQL statement is being invoked from a PHP script - the first form is not only

Java binary literals - Value -128 for byte

橙三吉。 提交于 2020-01-10 03:50:30
问题 Since SE 7 Java allows to specify values as binary literal. The documentation tells me 'byte' is a type that can hold 8 Bit of information, the values -128 to 127. Now i dont know why but i cannot define 8 bits but only 7 if i try to assign a binary literal to a byte in Java as follows: byte b = 0b000_0000; //solves to the value 0 byte b1 = 0b000_0001; //solves to the value 1 byte b3 = 0b000_0010; //solves to the value 2 byte b4 = 0b000_0011; //solves to the value 3 And so on till we get to

memory address literal

夙愿已清 提交于 2020-01-03 13:09:35
问题 Given a literal memory address in hexadecimal format, how can I create a pointer in C that addresses this memory location? Memory addresses on my platform (IBM iSeries) are 128bits. C type long long is also 128bits. Imagine I have a memory address to a string (char array) that is: C622D0129B0129F0 I assume the correct C syntax to directly address this memory location: const char* const p = (const char* const)0xC622D0129B0129F0ULL I use ULL suffix indicate unsigned long long literal. Whether