i want to include value of i hex format in c.
for(i=0;i<10;i++)
sprintf(s1"DTLK\x%x\xFF\xFF\xFF\xFF\xFF\xFF",i);
but the above code outputs an error: \x used with no following hex digits
Pls any one suggest me a proper way....
Supposing you don't want to literally have \x00..\x0A, but the corresponding byte, you need
sprintf(s1, "DTLK%c\xFF\xFF\xFF\xFF\xFF\xFF",i);
while inserting \x%x would be at the wrong abstraction level...
If, OTOH, you really want to literally have the hex characters instead of the bytes with the named hey characters as their representation, the other answers might be more helpful.
You need to escape the slash on front of the \x:
sprintf(s1"DTLK\\x%x\xFF\xFF\xFF\xFF\xFF\xFF",i);
// ^------- Here
Depending on what output you would like to achieve, you may need to escape the remaining slashes as well.
Currently, the snippet produces a sequence of six characters with the code 0xFF. If this is what you want, your code fragment is complete. If you would like to see a sequence of \xFF literals, i.e. a string that looks like \x5\xFF\xFF\xFF\xFF\xFF\xFF when i == 5, you need to escape all slashes in the string:
sprintf(s1"DTLK\\x%x\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF",i);
// ^ ^ ^ ^ ^ ^ ^
Finally, if you would like the value formatted as a two-digit hex code even when the value is less than sixteen, use %02x format code to tell sprintf that you want a leading zero.
\x expects a hex value like \xC9.
If you want to include \x in your output, you need to escape \ with \\:
sprintf(s1"DTLK\\x%x\xFF\xFF\xFF\xFF\xFF\xFF",i);
sprintf(s1"DTLK\\x%x\xFF\xFF\xFF\xFF\xFF\xFF",i);
// ^------- Here
Depending on what output you would like to achieve, you may need to escape the remaining slashes as well.
Currently, the snippet produces a sequence of six characters with the code 0xFF. If this is what you want, your code fragment is complete. If you would like to see a sequence of \xFF literals, i.e. a string that looks like \x5\xFF\xFF\xFF\xFF\xFF\xFF when i == 5, you need to escape all slashes in the string:
sprintf(s1"DTLK\\x%x\\xFF\\xFF\\xFF\\xFF\\xFF\\xFF",i);
// ^ ^ ^ ^ ^ ^ ^
Finally, if you would like the value formatted as a two-digit hex code even when the value is less than 16, use %02x format code to tell sprintf that you want a leading zero.
Related
I'm trying to modify one byte of my structure with the following code :
struct example *dev;
PRINT_OPAQUE_STRUCT(dev);
sprintf((char*) dev + 24, "%x",1);
PRINT_OPAQUE_STRUCT(dev);
The PRINT_OPAQUE_STRUCT is just printing the content of the structure, and is defined in this other topic :
Print a struct in C
The output of this program is :
d046f64f20b3fb4f00000000e047f64f00000000ffffffff000000
d046f64f20b3fb4f00000000e047f64f00000000ffffffff310000
I don't know why I have the value "31" written and not the value "01" as wanted. I tried to replace the second argument of sprintf with "%01x" but it didn't change anything. Anyone knows why?
Thanks!
Well, you are formatting the value 1 as a string. That's what sprintf does. 0x31 is the character code for the character '1'. If you just want to write the byte value 0x01 into your struct, then you don't need sprintf. Just do this:
*((char*)dev + 24) = 1;
or (the same, but with slightly different syntax):
((char*)dev)[24] = 1;
Note also, like one comment below says, sprintf will not just write one single byte. Since it writes a string, and C strings are null-terminated, it will also write a null character ('\0', 0x00) right after the '1'.
I don't know why I have the value "31" written and not the value "01" as wanted.
The reason you see 31 is that your chain of functions interprets the value 1 twice:
First, sprintf interprets it as a character representing a hex digit
Second, PRINT_OPAQUE_STRUCT interprets the value again, now as a hex number
Essentially, sprintf converts 1 to its character representation, which is '1'. On your system, its code is 0x31, hence the output that you get.
You need to remove one of these two interpretations to get your code to print 1. To remove the first interpretation, simply assign 1 to the pointer, like this:
((char*)dev)[24] = 1;
To remove the second interpretation, print with %c instead of %x in PRINT_OPAQUE_STRUCT (which may not be possible).
I am trying to printf a string that shows a temperature table
printf("TABLE 24A (20\°C)");
The degree sign is a constant I have defined as 0xDF so the the string looks like this: "TABLE 24A (20\xDF C)"
This works but looks incorrect because of the space between the \xDF and the C.
If I remove the space the compiler issues a warning hex escape sequence out of range.
If I modify the string to "TABLE 24A (20\xDF\C)" I get the correct result but the compiler issues warning unknown escape sequence: '\C'
Is there a way to get rid of the warnings but lose the space between the two characters?
You can take advantage of the fact that consecutive string literals are automatically concatenated:
printf("**TABLE 24A (20\xDF" "C)**");
This prevents the parser from consuming more characters for the escape sequence than you want.
You could also pass in the character as a parameter and use the %c format specifier to print it:
printf("**TABLE 24A (20%cC)**", '\xDF');
\x escape sequences consume as many adjacent hex digits as possible. The C is being parsed as a hex digit.
With \x, you could combine two adjacent string literals.
printf("**TABLE 24A (20\xDF""C)**");
Or use a \unnnn Unicode escape, which is limited to four hex characters.
printf("**TABLE 24A (20\u00DFC)**");
Or octal \nnn:
printf("**TABLE 24A (20\337C)**");
My problem is fairly simple. See the piece of code here:
fprintf(fpOut, "%01X",ciphertext[s] ^ test[0]);
fprintf(fpOut, "%01X",ciphertext[s+1] ^ test[1]);
//the array test[] is a previously defined array containing characters, so is ciphertext[]
if I run the code above, I print into the file a sequence of hexadecimal characters (1AB289DF...)
However, if I try to print on the screen, I get gibberish. All I am asking for is a way to save those characters I am able to print into the file in order to use them later. I do not want to save them all into the file and then reopen it and read them again. Any ideas?
From How to display hexadecimal numbers in C?, try using printf("%08X", hex_example)
This is more of a formatting issue, if you are still unsure how to do this provide more code on how you would go about outputting the hex values to the terminal.
(0) pads the numbers to the left of your hex value.
(08) gives the width of your values (i.e. how long your display will be)
(X) identifier for hexadecimal values.
Not sure I am using the right terminology here, but I need the print or deparse methods use C notation (e.g. "\x05" instead of "\005" ) when escaping bytes out of the regular character set.
x <- "This is a \x05 symbol"
print(x)
[1] "This is a \005 symbol"
Is there a native way to accomplish this?
I need this for generating BSON: http://bsonspec.org/#/specification. All of the examples explicitly use \x05 notation.
Hacking into the internals of print seems a bad idea. Instead I think you should do the string escaping yourself, and eventually use cat to print the string without any extra escaping.
You can use encodeString to do the initial escaping, gregexpr to identify octal \0.. escapes, strtoi to convert strings representing octal numbers to those numbers, sprintf to print numbers in hexadecimal, and regenmatches to operate on the matched parts. The whole process would look something like this:
inputString <- "This is a \005 symbol. \x13 is \\x13."
x <- encodeString(inputString)
m <- gregexpr("\\\\[0-3][0-7][0-7]", x)
charcodes <- strtoi(substring(regmatches(x, m)[[1]], 2, 4), 8)
regmatches(x, m) <- list(sprintf("\\x%02x", charcodes))
cat(x, "\n")
Note that this approach will convert octal escapes like \005 to hexadecimal escapes like \x05, but other escape sequences like \t or \a won't be affected by this. You might need more code to deal with those as well, but the above should contain all the ingredients you need.
Note that the BSON specification you refer to almost certainly meant raw bytes, so as long as your string contains a character with code 5, which you can write as "\x05" in your input, and you write that string to the desired output in binary mode, it shouldn't matter at all how R prints that string to you. After all, octal \005 and hexadecimal \x05 are just two representations of the same byte you'll write.
Does cat suit your needs? Note, you have to escape the backslash:
> x <- "This is a \\x05 symbol\n"
> cat(x)
This is a \x05 symbol
How do you escape the % sign when using printf in C?
printf("hello\%"); /* not like this */
You can escape it by posting a double '%' like this: %%
Using your example:
printf("hello%%");
Escaping the '%' sign is only for printf. If you do:
char a[5];
strcpy(a, "%%");
printf("This is a's value: %s\n", a);
It will print: This is a's value: %%
As others have said, %% will escape the %.
Note, however, that you should never do this:
char c[100];
char *c2;
...
printf(c); /* OR */
printf(c2);
Whenever you have to print a string, always, always, always print it using
printf("%s", c)
to prevent an embedded % from causing problems (memory violations, segmentation faults, etc.).
If there are no formats in the string, you can use puts (or fputs):
puts("hello%");
if there is a format in the string:
printf("%.2f%%", 53.2);
As noted in the comments, puts appends a \n to the output and fputs does not.
With itself...
printf("hello%%"); /* like this */
Use a double %%:
printf("hello%%");
Nitpick:
You don't really escape the % in the string that specifies the format for the printf() (and scanf()) family of functions.
The %, in the printf() (and scanf()) family of functions, starts a conversion specification. One of the rules for conversion specification states that a % as a conversion specifier (immediately following the % that started the conversion specification) causes a '%' character to be written with no argument converted.
The string really has 2 '%' characters inside (as opposed to escaping characters: "a\bc" is a string with 3 non null characters; "a%%b" is a string with 4 non null characters).
Like this:
printf("hello%%");
//-----------^^ inside printf, use two percent signs together
You can use %%:
printf("100%%");
The result is:
100%
You are using the incorrect format specifier. You should use %% for printing %. Your code should be:
printf("hello%%");
Read more all format specifiers used in C.
The backslash in C is used to escape characters in strings. Strings would not recognize % as a special character, and therefore no escape would be necessary. printf is another matter: use %% to print one %.
You can simply use % twice, that is "%%"
Example:
printf("You gave me 12.3 %% of profit");
Yup, use printf("hello%%"); and it's done.
The double '%' works also in ".Format(…).
Example (with iDrawApertureMask == 87, fCornerRadMask == 0.05):
csCurrentLine.Format("\%ADD%2d%C,%6.4f*\%",iDrawApertureMask,fCornerRadMask) ;
gives the desired and expected value of (string contents in) csCurrentLine;
"%ADD87C, 0.0500*%"