How to assign a hexadecimal value to an unsigned char variable in a C program [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The hexadecimal value that I want to store in a variable is of length 32.
"DC4938C31B9E8B30F32FC0F5EC894E16".
I also want to then print this value but I don't know the format specifier in printf for unsigned char.

That's a string of 32 characters, being hexadecimal digits. That makes 16 bytes, or 128 bits.
m0skit0 told you how to store it in a string. If you actually want to store that in an integer, you'd need something like unsigned long long intvar = strtoull( stringvar, NULL, 16 ) - provided "long long" on your machine can stomach 128 bits.
But what you want is something completely different, which became clear only after you linked to that other question. (It's really bad to take something out of context like that, especially if you are confused about what you're actually doing.)
If you take a look at the API documentation, you will see that the parameter you are looking at is a pointer to DES_cblock. That's not a string, and not an integer. Have a look at seed and ivsetup in that other question, how they are initialized, and think for a minute.

That's not a char. It's a string:
unsigned char* value = "DC4938C31B9E8B30F32FC0F5EC894E16";
printf("%s\n", value);
Also, if this is a number I strongly do not suggest you transforming it into a string or char, since strings are slower and prone to more coding errors than numbers.
Numbers represented as hexadecimal (and not hexadecimal numbers, all numbers are hexadecimal, it's just a representation of the value) do not have any characters. Again, it's a number. You can of course convert it to a string (as with any other number) but to do so you should have a strong reason (e.g. the algorithm you want to use is faster with strings than numbers).
I suggest you reading about representation of numbers. A lot of new programmers have problems with this topic.

Related

Representation of a C binary file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For a homework assignment I created a simple compression/decompression program that makes use of a naive implementation of run-length encoding. I've gotten my program working; compressing and decompressing any text file with a pretty large number of characters (e.g. the program source) works flawlessly. As an experiment I tried to compress/decompress the binary of the compression program itself. This resulted in a file that was much smaller than the original binary, and is obviously un-runnable. What is causing this data-loss?
My assumption was that it's related to how binary files are represented, but I can't figure much out past that.
Possible issues:
Your program opens the binary file in the text mode, which damages the '\r' and '\n' bytes
Your program incorrectly handles zero bytes, treating them as ends of strings ('\0') and not as data of its own
Your program uses char (that is actually signed char) for the bytes of data and correctly works only with non-negative values, which ASCII chars of English text are, but fails to work with arbitrary char/byte values, which may be negative
Your program has an overflow somewhere which shows up only on big files
Your program has some other data-dependent bug
If the platform is linux (as the question is tagged), there's no difference between binary and text modes. So it shouldn't be that; but even so, the files should be opened as binary.
I suspect that your problem is the program treats '\0' characters as terminators (or otherwise specially) instead of as valid data.

Dynamically declare variables/structure in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?
No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.
Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.

Large number store in float type in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
The problem I got is if I assign a large number to a float type, for example, float f = 1.0e20 then I print it to the screen with printf("f = %f\n",f), then on the screen it will be f = 100000002004087730000.000000. Could anyone tell me why the number display is not 100000000000000000000.000000. Thanks in advance.
The number you are using has no exact representation in the type you are storing it. As a result, the answers will never be exactly right.
By analogy, consider a computer that used 6 decimal digits. The best you can do for 1/3 is .333333 But then 3 * (1/3) != 1. Oh well.
And what about 2/3? If you use .666667 then 2/3 != 2 * (1/3). If you use .666666 then 1/3 + 2/3 != 1. Oh well.
That's just the way it is with floating point numbers.
Summarizing, floating point variables are represented as a aproximation of a fixed number which is later scaled with an exponent. In your case, the number is too big to being stored in a accurate way. Try using a double instead and printing it out with %lf. It'll be more accurate, although don't expect a real equality.

How long it would take you to make a working code to print out factorial of very very large numbers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I was given this coding question in interview:
given a very very large number (say more than long or any in-built types) print out its factorial. you can not assume a max limit anywhere in the program.I had to make a working code on computer and during interview.
I am really curious, how long on average would it take for others?
this is subjective question but an average will set some ballpark figures and a benchmarks for such a coding question.
What I did?
I chose C and represented number by a linked list of characters (containing a single digit). though perhaps it can be made more efficient to store chunks in int/long and do int arithmetic than store it in chunk of characters.
I took 2 hours and spat out a code with things in place, major fns coded, but then interviewer said she wanted a completely working one and asked me to do it offline and mail it to her.
The good solution is to write a BigInt class that supports addition and mutilplication only. The number shouldn't be kept in base 10, rather in base 10000, i.e. each digit is a number 0-9999. Writing this is about 50-60 lines of code which should be relatively quick. I would also go with vector rather than list
Of course if you're not allowed to use an existing big int class.
Check the following links
calculate-the-factorial-of-an-arbitrarily-large-number-showing-all-the-digits
http://www.daniweb.com/software-development/cpp/code/216490
calculating-factorial-of-large-numbers-in-c
I'd start by suggesting a straight factorial in Common Lisp as Lisp already supports arbitrary precision arithmetic.
Assuming "no max limits anywhere" is a bit disingenuous since the computer has limited memory / disk space in any case but I understand the general gist.
Barring those two points of argument, you need to implement some sort of ADT for an arbitrary-sized number like Armen suggests.
To what accuracy? My first impulse would be to simply use Stirling's approximation.
There's no way I'm going to implement factorial for longs (bigger than ints) not to say above that.. It's totally pointless. No matter how fast my biginter library is.
32bit -> 4G, product of the numbers just from 1G till 4G.. well, lets just get a quick (under)estimation for the number of digits: 3G * 9 = 27G. rough estimation for the storage for that: 2^10=1024, so for 3 digits you need 1.25 bytes.. that's 11.25G. remember, this was a serious underestimation...

How to generate 8 byte hex value? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I want to generate this sequential data in C:
data_packet[1] = 0706050403020100 (seed_value)
next
data_packet[2] = 0f0e0d0c0b0a0908
Next will be the next 8 hexadecimal characters and so on for say 100 bytes. How can I do it? Can we do it using character array?
You don't want to use a char array since char may or may not be signed (implementation defined). If you are playing with hexadecimal numbers from 0x00 to 0xFF, I strongly recommend using an unsigned char array.
Looks like the values in the array are sequential, from 0 to N. This indicates using a for loop.
The array is a fixed size of 8 bytes. Hmmm, another good candidate for a for loop.
The hard part of your task is the direction of the bytes. For example are filling in the array starting at position 7 or at position 0?
So here is some psuedo code to help you along:
For each value from 0 to N do:
begin
for array position from 0 to 7 do: // or from 7 to 0
put 'value' into the array at 'position';
call function to process the array.
end
For more fun, change the functionality of "put 'value'" to "put random value".
If you want us to actually write your program, let us know. I could use the extra money. :-)

Resources