qq_20258523
qq_20258523
采纳率2.6%
2019-03-13 14:11

Android 一串字符串怎么转换成16进制数组

40
  • android

一串字符串怎么转换成16进制数组,什么方法啊。。求指点。,,,,,,,,,,,,,,,,,,,,,,,,,,,,

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_44770557 天空中飞舞的香瓜 2年前

    class Program
    {
    static void Main(string[] args)
    {
    string str = "Hello,C#!!!";
    //调用。
    string result = StringToHex16String(str);
    Console.WriteLine(string.Format("将普通字符串:{0}转换成16进制字符串是:{1}", str, result));
    Console.ReadKey();
    }

    ///
    /// 此方法用于将普通字符串转换成16进制的字符串。
    ///
    /// 要转换的字符串。
    ///
    public static string StringToHex16String(string _str)
    {
    //将字符串转换成字节数组。
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(_str);
    //定义一个string类型的变量,用于存储转换后的值。
    string result = string.Empty;
    for (int i = 0; i < buffer.Length; i++)
    {
    //将每一个字节数组转换成16进制的字符串,以空格相隔开。
    result += Convert.ToString(buffer[i], 16) + " ";
    }
    return result;
    }
    }

    点赞 评论 复制链接分享
  • weixin_40584261 TinerSky 2年前

    可以参考一下下面的代码:

    https://www.cnblogs.com/xiezunxu/articles/8968605.html

    点赞 评论 复制链接分享
  • qq_28945911 小明520 2年前

    /*String转byte数组*/
    public static byte[] Stringtobytes(String s) {
    byte[] present = {};
    if (Integer.parseInt(s) >= 16) {
    present = hexString2Bytes(Integer.toHexString(Integer.parseInt(s)));
    }else if(Integer.parseInt(s) == 0){
    present = new byte[]{0x00};
    }else if(Integer.parseInt(s) == 1){
    present = new byte[]{0x01};
    }else if(Integer.parseInt(s) == 2){
    present = new byte[]{0x02};
    }else if(Integer.parseInt(s) == 3){
    present = new byte[]{0x03};
    }else if(Integer.parseInt(s) == 4){
    present = new byte[]{0x04};
    }else if(Integer.parseInt(s) == 5){
    present = new byte[]{0x05};
    }else if(Integer.parseInt(s) == 6){
    present = new byte[]{0x06};
    }else if(Integer.parseInt(s) == 7){
    present = new byte[]{0x07};
    }else if(Integer.parseInt(s) == 8){
    present = new byte[]{0x08};
    }else if(Integer.parseInt(s) == 9){
    present = new byte[]{0x09};
    }else if(Integer.parseInt(s) == 10){
    present = new byte[]{0x0a};
    }else if(Integer.parseInt(s) == 11){
    present = new byte[]{0x0b};
    }else if(Integer.parseInt(s) == 12){
    present = new byte[]{0x0c};
    }else if(Integer.parseInt(s) == 13){
    present = new byte[]{0x0d};
    }else if(Integer.parseInt(s) == 14){
    present = new byte[]{0x0e};
    }else if(Integer.parseInt(s) == 15){
    present = new byte[]{0x0f};
    }

    return present;
    

    }

    点赞 评论 复制链接分享
  • qq_38181513 qq_38181513 2年前

    java :
    /**
    * 将字符串转为指定编码的16进制
    *
    * @param str
    * @return
    */
    public static String encode(String str) throws Exception {
    String hexString = "0123456789ABCDEF";
    //根据编码获取字节数组
    byte[] bytes = str.getBytes("GBK");
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    //将字节数组中每个字节拆解成2位16进制整数
    for (int i = 0; i < bytes.length; i++) {
    sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
    sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
    }
    return sb.toString();
    }

        //字符串转化数组
        String str = "0,1,2,3,4,5";
    

    String[] arr = str.split(","); // 用,分割
    System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4, 5]

    点赞 评论 复制链接分享

相关推荐