how to convert "0xbff18c08" to 0xbff18c08 int? - c

I am looking for an native function from the C libraries or code example to implement it: I have a string that contains byte values that I want to convert it to int value. how do I this? I tried using atoi() and atol() but it get only 0 from 0xbff18c08. Thanks.

Then you are likely looking for strtoul(str, NULL, 16).

The wording on your question is kind of strange. Do you have a string?
You can check into sscanf:
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

Related

Test for numbers in string in C

I want to make a function that tests for numbers in string values in C. I have no idea where to start so I did not provide any code. How can I do this?
Also, how can I make this work for hexadecimal?
<ctype.h> provides two functions, isalpha and isdigit, that may be what you're looking for. Just iterate on the characters of the string and check if isdigit() ever returns true.

J2ME Integer to Hex String

I need to convert from an integer to a hexadecimal string (Character string) in Java ME (v3.2). I tried to try String.format() but that is not supported in this version. Any other alternatives?
Sample code would be helpful.
You can use Integer.toHexString.

Decimal to hexadecimal conversion: issues with sprintf?

I have a requirement, part of which needs conversion from decimal to hex.
I prefer the following way to do that thing as below:
sprintf(mcc,"%x",n);
n here will be an integer.
But my peer says it is not a good way to do that.
Instead he says to manually convert the decimal using a function,
but I did not get a good explanation about why not to use sprintf.
Are there any problems with the above method I am using?
Should I go for a function which manually converts the decimal number into hex?
As long as you make sure that the buffer pointed to by mcc is big enough for the resulting string, I see no problem.
If you're on a machine which supports the GNU extensions then you can use asprintf as well. This will allocate heap memory, store the string in it and then give you a pointer to this. Just make sure you free the string when you're done with it.
The problem with sprintf is that it's hard to guarantee your buffer is big enough. Use snprintf instead.
In this case the maximum possible output length is small and easily calculated, but it's not always this easy, and my guess is that this is why your friend told you not to use sprintf.

Print a number in base 4

Lately I had a task that included printing base-4 representation of a number. Since I didn't find a function to do it for me, I implemented it (which is not so hard of course), but I wonder, is there a way to do it using format placeholders?
I'm not asking how to implement such function, but if such function / format placeholder already exists?
There is no standard C or C++ function, but you may be able to use itoa
The closest you could get to doing it with printf is using snprintf to convert it to hex, then a lookup table to convert hex digits to pairs of base-4 digits. :-)
No, not in the Standard C library.
I think that printf can handle only decimal, hexadecimal and octal values.
So i think no.

Converting a UNICODE_STRING to ANSI or vice versa in C

I have a UNICODE_STRING that I would like to compare to a null-terminated ANSI string to check if they are the same. I'm using C. I would like to avoid including winternl.h for RtlInitUnicodeString.
What is the preferred method doing this?
Or, alternatively, is there any problem with me using MultiByteToWideChar() to convert the ANSI string to a wide-character representation and then comparing that to the UNICODE_STRING.buffer (with the understanding that the buffer might not be null-terminated)?
WideCharToMultiByte seems the more logical route. It can handle strings that aren't zero-terminated and produces a terminated one. And it tries to do something meaningful with codepoints that don't have a character in the system code page. Then just strcmp().
I would just convert the ANSI string using MultiByteToWideChar(). The CompareString() function takes length parameters for each string, so no worries about the missing null-terminator.
Just be careful about which parameters take or return bytes verses characters, and there should be no problems using these functions.

Resources