在Java中int类型强转为byte类型,强转为byte之后,数据超出byte的表述范围是如何计算的?...

public class Aa {

public static void main(String[] args) {  byte i = 88;  byte r = 68;  byte f = (byte) (i + r);    System.out.println(f);

}

}

输出结果为-100

  88+68=156;二进制表示为:1001 1100,进行强制转换为byte后,因为byte是有符号的,取值范围为:-128-127;1001 1100是一byte数的补码,我们将它转为原码,即减一后再取反,但符号位不能变,得到:1110 0100,这个数也就是-100了

总结 :(88+68)-127+(-128)-1

 

转载于:https://www.cnblogs.com/kendn/p/8284358.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码将byte[]类型数据转为pdf,并在指定坐标位置添加文字: ``` import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; public class PDFTest { public static byte[] addTextToPdf(byte[] pdfBytes, String text, float x, float y) throws Exception { PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfBytes)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, baos); PdfContentByte canvas = stamper.getOverContent(1); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); canvas.beginText(); canvas.setFontAndSize(bf, 12); canvas.showTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0); canvas.endText(); stamper.close(); reader.close(); return baos.toByteArray(); } public static byte[] byteToPdf(byte[] bytes) throws Exception { Rectangle pageSize = new Rectangle(PageSize.A4); Document document = new Document(pageSize, 50, 50, 50, 50); PdfWriter.getInstance(document, new ByteArrayOutputStream()); document.open(); PdfContentByte cb = PdfWriter.getInstance(document, new ByteArrayOutputStream()).getDirectContent(); PdfImportedPage page = null; PdfReader reader = new PdfReader(bytes); for (int i = 1; i <= reader.getNumberOfPages(); i++) { page = PdfWriter.getInstance(document, new ByteArrayOutputStream()).getImportedPage(reader, i); cb.addTemplate(page, 0, 0); } document.close(); return ((ByteArrayOutputStream) cb.getUnderlyingOutputStream()).toByteArray(); } public static void main(String[] args) throws Exception { byte[] pdfBytes = new byte[]{}; // 读取pdf文件的byte数组 byte[] addedPdfBytes = addTextToPdf(pdfBytes, "Hello World", 100, 100); // 在指定坐标位置添加文字 byte[] resultBytes = byteToPdf(addedPdfBytes); // 将byte[]类型数据转为pdf // 处理resultBytes } } ``` 其,`addTextToPdf`方法用于在pdf文件的第一页上指定坐标位置添加文字,`byteToPdf`方法用于将byte[]类型数据转为pdf。在使用时,先读取pdf文件的byte数组,然后调用`addTextToPdf`方法添加文字,最后调用`byteToPdf`方法将byte[]类型数据转为pdf。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值