简体   繁体   中英

How to Convert ASCII to Hex

How can I convert ASCII values to hexadecimal and binary values (not their string representation in ASCII)? For example, how can I convert the decimal value 26 to 0x1A?

So far, I've tried converting using the following steps (see below for actual code):

  1. Converting value to bytes
  2. Converting each byte to int
  3. Converting each int to hex, via String.toString(intValue, radix)

Note: I did ask a related question about writing hex values to a file.

Clojure code:

(apply str
  (for [byte (.getBytes value)]
    (.replace (format "%2s" (Integer/toString (.intValue byte) 16)) " " "0")))))

Java code:

Byte[] bytes = "26".getBytes();
for (Byte data : bytes) {
    System.out.print(String.format("%2s", Integer.toString(data.intValue(), 16)).replace(" ", "0"));
}
System.out.print("\n");

Hexadecimal, decimal, and binary integers are not different things -- there's only one underlying representation of an integer. The one thing you said you're trying to avoid -- "the ASCII string representation" -- is the only thing that's different. The variable is always the same, it's just how you print it that's different.

Now, it's not 100% clear to me what you're trying to do. But given the above, the path is clear: if you've got a String , convert it to an int by parsing (ie, using Integer.parseInt() ). Then if you want it printed in some format, it's easy to print that int as whatever you want using, for example, printf format specifiers.

If you actually want hexadecimal strings, then (format "%02X" n) is much simpler than the hoops you jump through in your first try. If you don't, then just write the integer values to a file directly without trying to convert them to a string.

Something like (.write out (read-string string-representing-a-number)) should be sufficient.

Here are your three steps rolled up into one line of clojure:

(apply str (map #(format "0x%02X " %) (.getBytes (str 42))))
  • convert to bytes (.getBytes (str 42))
  • no actual need for step 2
  • convert each byte to a string of characters representing it in hex

or you can make it look more like your steps with the "thread last" macro

(->> (str 42)                    ; start with your value
     (.getBytes)                 ; put it in an array of bytes
     (map #(format "0x%02X " %)) ; make hex string representation
     (apply str))                ; optionally wrap it up in a string
static String decimalToHex(String decimal, int minLength) {
  Long n = Long.parseLong(decimal, 10);
  // Long.toHexString formats assuming n is unsigned.
  String hex = Long.toHexString(Math.abs(n), 16);
  StringBuilder sb = new StringBuilder(minLength);
  if (n < 0) { sb.append('-'); }
  int padding = minLength - hex.length - sb.length();
  while (--padding >= 0) { sb.append('0'); }
  return sb.append(hex).toString();
}
//get Unicode for char
char theChar = 'a';
//use this to go from i`enter code here`nt to Unicode or HEX or ASCII
int theValue = 26; 
String hex = Integer.toHexString(theValue);
while (hex.length() < 4) {
    hex = "0" + hex;
    }
String unicode = "\\u" + (hex);
System.out.println(hex);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM