java二进制,字节数组,字符,十六进制,BCD编码转换

  1. // 整数到字节数组转换 
  2.  public static byte[] int2bytes(int n) { 
  3.   byte[] ab = new byte[4]; 
  4.   ab[0] = (byte) (0xff & n); 
  5.   ab[1] = (byte) ((0xff00 & n) >> 8); 
  6.   ab[2] = (byte) ((0xff0000 & n) >> 16); 
  7.   ab[3] = (byte) ((0xff000000 & n) >> 24); 
  8.   return ab; 
  9.  } 
  10.  // 字节数组到整数的转换 
  11.  public static int bytes2int(byte b[]) { 
  12.   int s = 0
  13.   s = ((((b[0] & 0xff) << 8 | (b[1] & 0xff)) << 8) | (b[2] & 0xff)) << 8 
  14.     | (b[3] & 0xff); 
  15.   return s; 
  16.  } 
  17.  // 字节转换到字符 
  18.  public static char byte2char(byte b) { 
  19.   return (char) b; 
  20.  } 
  21.  private final static byte[] hex = "0123456789ABCDEF".getBytes(); 
  22.  private static int parse(char c) { 
  23.   if (c >= 'a'
  24.    return (c - 'a' + 10) & 0x0f
  25.   if (c >= 'A'
  26.    return (c - 'A' + 10) & 0x0f
  27.   return (c - '0') & 0x0f
  28.  } 
  29.  // 从字节数组到十六进制字符串转换 
  30.  public static String Bytes2HexString(byte[] b) { 
  31.   byte[] buff = new byte[2 * b.length]; 
  32.   for (int i = 0; i < b.length; i++) { 
  33.    buff[2 * i] = hex[(b[i] >> 4) & 0x0f]; 
  34.    buff[2 * i + 1] = hex[b[i] & 0x0f]; 
  35.   } 
  36.   return new String(buff); 
  37.  } 
  38.  // 从十六进制字符串到字节数组转换 
  39.  public static byte[] HexString2Bytes(String hexstr) { 
  40.   byte[] b = new byte[hexstr.length() / 2]; 
  41.   int j = 0
  42.   for (int i = 0; i < b.length; i++) { 
  43.    char c0 = hexstr.charAt(j++); 
  44.    char c1 = hexstr.charAt(j++); 
  45.    b[i] = (byte) ((parse(c0) << 4) | parse(c1)); 
  46.   } 
  47.   return b; 
  48.  }  

原文引自:http://blog.csdn.net/fenglibing/archive/2008/10/09/3044079.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下的Java代码来解决这个问题: ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MinimumDistanceFinder { public static void main(String[] args) { String[] strs = {"abc", "cde", "bcd", "ac", "bcd", "ac"}; String str1 = "abc"; String str2 = "ac"; int minDistance = findMinDistance(strs, str1, str2); System.out.println("最小距离为:" + minDistance); } public static int findMinDistance(String[] strs, String str1, String str2) { // 创建一个HashMap来存储每个字符串在数组中的索引列表 Map<String, List<Integer>> indicesMap = new HashMap<>(); // 遍历数组,将每个字符串出现的索引添加到对应的列表中 for (int i = 0; i < strs.length; i++) { String word = strs[i]; if (!indicesMap.containsKey(word)) { indicesMap.put(word, new ArrayList<>()); } indicesMap.get(word).add(i); } // 获取str1和str2在数组中的索引列表 List<Integer> indices1 = indicesMap.get(str1); List<Integer> indices2 = indicesMap.get(str2); // 如果str1或str2为null,或不在strs中,则返回-1 if (indices1 == null || indices2 == null) { return -1; } int minDistance = Integer.MAX_VALUE; // 计算最小距离 for (int i : indices1) { for (int j : indices2) { minDistance = Math.min(minDistance, Math.abs(i - j)); } } return minDistance; } } ``` 这个程序通过使用一个HashMap来存储每个字符串在数组中出现的索引列表。然后,它找到str1和str2在数组中的索引列表,并计算它们之间的最小距离。如果str1或str2为null,或者它们不在strs中,程序将返回-1。在给定的示例中,程序将输出最小距离为3。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值