Appending a byte to String in java using escape sequence -


i wish append byte i.e. 16 = 0x10 string, using escape sequence in single line of code:

string appendedstring = new string('\16'+"string"); 

this results in hex representation of appendedstring = 0x0e,0x74,0x72,0x69,0x6e,0x67

using \2 this:

string appendedstring = new string('\2'+"string"); 

works fine resulting in hex representation of appendedstring = 0x02,0x74,0x72,0x69,0x6e,0x67

using \10:

string appendedstring = new string('\10'+"string"); 

results in hex representation of appendedstring = 0x08,0x74,0x72,0x69,0x6e,0x67

someone may kindly explain , suggest solution. thanks.

the problem using octal escape. java language specification, section 3.10.6, defines escapes, including octal escapes.

octalescape: \ octaldigit \ octaldigit octaldigit \ zerotothree octaldigit octaldigit

so, \16 character 14 in decimal, or 0x0e in hexadecimal.

the character \2 remains 2 in decimal , hexadecimal.

the character \10 8 in decimal, or 0x08 in hexadecimal.

to use hexadecimal escapes, use unicode escape, defined in jls section 3.3:

unicodeescape:

\ unicodemarker hexdigit hexdigit hexdigit hexdigit 

unicodemarker:

u unicodemarker u 

such \u0016, \u0002, , \u0010.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -