How do I save a hexadecimal value as a character in C? - c

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.

Related

Print number with zero on first place

How i can print for example?
int a = 0145236
When I want to use it, it always gives me a octal number.
Whenever you put zero in front of your number it is printed in octal only. That is the way c works.
However, if you take input from the user and supposing user entered 0123456 it is stored in your variable as 123456 so just don't add 0 in the beginning of your integer number when hard coding.
In case you need to add leading zeros in your number this may help Printing leading 0's in C?

how to print formatted output on terminal using C?

I have written a code for a Library system in C. And I want to show the output in following manner on terminal on Linux. I tried with "\t" but the output gets disturbed when the string size varies. I want to print it in fixed manner no matter what string size comes.
I want to print output like below-
I tried to print this using "\t" but the format gets disturbed when the string length of book or author gets smaller or larger. Can somebody help me with this??
Print with fixed character size. Here it is 7,11 and 10 for columns. Refer this for more details this
printf("Column1 Column2 Column3\n");
printf("%7d%11s%10d\n", 100, "String1", 9348);
printf("%7d%11s%10d\n", 23, "String2", 214);
use printf like this :
printf("%-25s|\n", "a string");
printf("%-25s|\n", "another string");
(the - in %-25s is use to left-justifies your text)
not a linux user (hope we are talking about monospace output) but my experienceis that tab has usually configurable size so if you format for 6 character length and someone have 4 character tab the result will be bad. The safest is to use spaces. You can use formated output like:
printf("float number: 8.3%f",7.56);
But that is not always a good choice for example sometimes negative sign mess up things ...
I usually handle such formatting my self with use of string variables:
line = ""
item = "single unformated text value"
compute length of item
add missing spaces (before or after) to line or item
add item to line
loop #2 for all items
output line
loop #1 for all lines

String input into int array -- C

I'm trying to write a program that will convert a floating point number into its IEEE 754 single representation and vice versa. I'm stuck on how to take the input, which will be in the form "0xABCD123" or "2.83234e-2" (with the hex representation always preceded by "0x"), and take each element and put it into a char array.
For example, the input "0xABCD123" will be in a char array of size 10. I will remove the "0" and "x" elements, and then go through each element individually and convert into floating point from there with the use of a for loop.
Getting the argument from the command line is simple enough I think. The argument is stored in argv[1], then I save that into an initialized string variable called input. The problem is taking input and putting each element into a character array. The second problem is determining the length of the array if the input is not in IEEE format. I haven't found a function that determines the length of a string (removing decimals points and account for the 'e').
Also, a couple questions: Does C automatically recognize the "e-2" part of an input and translate that into the actual number? Is there some command I can use to do that?
How would type casting work here? If the input was "2.83234e3", and I wanted to save the part to the left of the decimal point as an int (because that would be easier to translate), and then the left as an int as well, how would I split up the number through type casting? Would it be something like input = (int)left;?

Read image file hex values and print to file

I am absolutely new to C, learning from a book. I have been searching on the net for how to read and write hex values but I can't find what I am looking for.
Basically I want to read an image file like jpg and write it out to a file verbatim, but my code doesn't do it.
#include<stdio.h>
int main()
{
unsigned long txt;
FILE *myimg, *img;
myimg=fopen("myimg.jpg","w");
fclose(myimg);
myimg=fopen("myimg.jpg","rb+");
img=fopen("img.jpg","rb");txt=0;
printf("Start");
while(!feof(img))
{
if(img==NULL)
{
printf("WTF1");
}
txt=fgetc(img);
fprintf(myimg,"%x",txt);
}
return(0);
}
The output file is different in size and when I look at it in a hex editor, there is no similarity. Can you tell me how it is done?
Assuming CHAR_BITS==8, each char holds a value in the range [0..255] (or [-128..127] if it is signed). This is represented in hex by [0x00..0xff]. Values between 0 and 15 can be represented as hex in a single character; other values will need two characters to represent a single char as hex.
fprintf(myimg,"%x",txt);
write the value of your char as hex. For values outside the range [0..15], it'll need to write 2 characters to represent a single char. (e.g. if txt==16, formatting it as hex will write the characters 1 then 0 to file.)
You need to use the %c format specifier instead
fprintf(myimg,"%c",txt);
Alternatively, it'd be clearer if you used fputc
fputc(myimg,txt);
What you have written is different from what you read.
You read a char, but you write a long value in text mode, they are quite different.
If you open your output file in a normal text editor and open your input file in a hex editor, they should look the same.
Just use fputc() instead of fprintf, you should get what you want.

how to include hex value in string using sprintf

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.

Resources