16进制码转换成图片(可用)

下面很长的东西为一张图片的16进制转码

	/**
	 * 
	 */
	@RequestMapping(value = LIST + "/testImg",method=RequestMethod.POST)
	@ResponseBody
	public String testImg(String imgByte){
		String path = "D://imgSave";
		File dir = new File(path);
		
		imgByte = "图片16进制代码";
		
		//检查目录
		if(!dir.isDirectory()){
			throw new BusinessException("上传目录不存在");
		}
		//检查目录写权限
		if(!dir.canWrite()){
			throw new BusinessException("上传目录没有写权限");
		}
		
		path = "D://imgSave//test.jpg"; //path到下面方法调用的时候需要有带记录下来的文件名
		
		try {
			com.dql.lms.system.util.HexToImage.toImage(imgByte, path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("十六进制转换图片报错");
			e.printStackTrace();
		}
		
		return "1";
	}



上面方法调用的方法



package com.dql.lms.system.util;

import java.io.File;
import java.io.FileOutputStream;

public class HexToImage {
    public static void toImage(String image, String path) throws Exception {  
    	
       HexToImage to=new HexToImage(); 
       /*InputStream is=new FileInputStream("f://cc.txt");  
       InputStreamReader isr = new InputStreamReader(is);  
       BufferedReader br = new BufferedReader(isr);  
       String str = null;  
       StringBuilder sb = new StringBuilder();  
       while ((str = br.readLine()) != null) {  
           //System.out.println(str);  
           sb.append(str);  
       }
       //str.replaceAll(" ", "");
       //String aaa = sb.toString();
       //aaa.replaceAll(" ", "");
       System.out.println(sb.toString().replaceAll(" ", ""));  
       //System.out.println(str);  
       to.saveToImgFile(sb.toString().replaceAll(" ", "").replace("<", "").replace(">", "").toUpperCase(),path);*/
       if (image != null) {  
    	   to.saveToImgFile(image.replaceAll(" ", "").replace("<", "").replace(">", "").toUpperCase(), path);  
        }
    }
    
    public void saveToImgFile(String src,String output){  
           if(src==null||src.length()==0){  
               return;  
           }  
           try{  
               FileOutputStream out = new FileOutputStream(new File(output));  
               byte[] bytes = src.getBytes();  
               for(int i=0;i<bytes.length;i+=2){  
                   out.write(charToInt(bytes[i])*16+charToInt(bytes[i+1]));  
               }  
               out.close();  
           }catch(Exception e){  
               e.printStackTrace();  
           }  
       }  
       private int charToInt(byte ch){  
           int val = 0;  
           if(ch>=0x30&&ch<=0x39){  
               val=ch-0x30;  
           }else if(ch>=0x41&&ch<=0x46){  
               val=ch-0x41+10;  
           }  
           return val;  
       }  
}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: STM32是一种集电路微控器芯片,通常用于嵌入式系统中。它支持多种不同之间的转换,包括十到十六转换。 十数是我们常见的数,可以用阿拉伯数字表示。而十六数则是一种更加紧凑的表示法,用16个数字(0-9,以及A-F)来表示数,十六的数字前面加“0x”的前缀,例如0x12表示十数18。 如果我们要将一个十转换十六,可以使用STM32内置的函数来完。例如,可以使用sprintf函数来将一个十数格式化输出为十六字符串。 具体的转换代码如下: ```c int decValue = 1234; char hexValue[5]; // 4 digits + null terminator sprintf(hexValue, "%04X", decValue); ``` 在上面的代码中,我们将十数1234转换了一个四位的十六字符串,存储在hexValue数组中。其中%04X表示对应输出一个四位的十六数,不足四位时用0补齐。 通过这种方法,我们可以方便地将十转换十六数,便于在STM32中行处理和显示。 ### 回答2: STM32是一款基于ARM Cortex-M架构设计的微控器。它提供了多种转换函数,使得十转换为十六数更加简单。 STM32中可用的函数包括itoa()和sprintf(),它们可以将一个整数转换为字符型数列,并把它们写入指定的输出缓冲区。itoa()函数可以把一个整数转换为指定的字符型数列,sprintf()函数可以把多个数据转换为指定的格式,包括十六。 在使用这些函数时,我们先要定义一个变量来存储要转换的整数,然后定义一个输出缓冲区用于存储转换后得到的十六数字。使用itoa()函数,我们可以将十数字转换为十六,并将结果存储到输出缓冲区中。使用sprintf()函数,我们可以直接将数字转换为十六字符串。 例如,将十数10转换为十六数: - 使用itoa()函数: ```c int a = 10; char b[10]; itoa(a, b, 16); ``` 这会把整数a转换为十六,并把结果存储到b中。此时b的值为"A"。 - 使用sprintf()函数: ```c int a = 10; char b[10]; sprintf(b, "%x", a); ``` 这会把整数a转换为十六字符串,并把结果存储到b中。此时b的值为"a"。 使用这些函数可以方便地实现十转十六转换,并且可以根据需要选择适当的函数以及输出格式。 ### 回答3: 在STM32单片机中,将十转换为十六数的方法如下: 1. 首先,将十数除以16,得到商和余数。 2. 如果商大于等于1,则连续对商行除以16,直到商小于1为止。将每一次得到的余数记录下来,并按照从低位到高位的顺序排列起来,即为所求的十六数。 3. 如果商小于1,则将余数直接转换为十六数即可。例如,余数为10,则表示为A;余数为15,则表示为F。 举个例子,将十数125转换为十六数: 125 ÷ 16 = 7·····13 7 ÷ 16 = 0·····7 所以,125的十六表示为7D。 在STM32单片机中,可以使用sprintf函数将十转换为十六数。例如,以下代码将整数num转换为16字符串: char str[10]; sprintf(str, "%x", num); 其中%x表示要将num转换为十六数。转换后的十六数存储在str数组中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值