J2ME Integer to Hex String - mobile

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.

Related

MD5 hash not creating all characters

i'm using MD5 hashing to encrypt passwords for a program. But it is not creating all the characters and that to some are unreadable.
Here is an screenshot.
link-http://i46.tinypic.com/2qvf2o2.jpg
Any help is appreciated
Thanks
Presumably you want to convert the array of bytes returned by MD5 to a hexidecimal string for display. Something like d131dd02c5e6eec4.
Here's how you can do that:
In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?
You're interpreting the bytes returned by MD5 as raw character data.
Since MD5 does not return bytes that represent characters, you get meaningless results.
What you're getting back is a binary value. So it's a bunch of raw bytes that may or may not map to valid characters in your default codepage. What you should do is convert the byte[] to hex. You can use something like Apache Commons Codec to encode this. http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#encodeHex(byte[])

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

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/

Converting Base64 to GMP integer

I have a scenario where the I get a Base64 (64 bit encoded) string. My requirement is to convert this string to gmp integer (mpz_t).
But according to GMP manual only "The base may vary from 2 to 62" for the function mpz_set_str() .
Is there any approach I can follow to perform a successful conversion?
One idea that struck me was to convert the Base64 to binary and then set the mpz_t variable using mpz_set_str with base 2.
Help would be really appreciated. Thanks.
GMP bases are not the same thing as base64 encoding. You're on the right track - apply the base64 decode, then use mpz_import on the result.

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.

Where did the name `atoi` come from?

In the C language where did they come up with the name atoi for converting a string to an integer? The only thing I can think of is Array To Integer for an acronym but that doesn't really make sense.
It means Ascii to Integer. Likewise, you can have atol for Ascii to Long, atof for Ascii to Float, etc.
A Google search for 'atoi "ascii to integer"' confirms this on several pages.
I'm having trouble finding any official source on it... but in this listing of man pages from Third Edition Unix (1973) collected by Dennis Ritchie himself, it does contain the line:
atoi(III): convert ASCII to integer
In fact, even the first edition Unix (ca 1971) man pages list atoi as meaning Ascii to Integer.
So even if there isn't any documentation more official than man pages indicating that atoi means Ascii to Integer (I suspect there is and I just haven't been able to locate it), it's been Ascii to Integer by convention at least since 1971.
I griefly believe that function atoi means ascii to integer.

Resources