字符串到字符数组Java [英] String to char array Java

查看:49
本文介绍了字符串到字符数组Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个难住了,我需要一些新鲜的眼光,我不知道为什么这段代码会这样做.

I am stumped on this and I need some fresh eyes, I'm not sure why this code is doing this.

String string = new String(new char[] {(char) 0x01, (char) 0x02, ... ,(char) 0xFC});

输出是它应该得到的一切,直到最后一个数字(0xFC)返回-4,我知道它是一个十六进制值,但如果我对十进制值 252 做同样的事情,它也会给我一个负数.我希望这只是一个简单的解决方案,我只是看不到.

The output is everything it should be up until the last number (the 0xFC) it returns a -4, I know its a hex value, but if I do the same with 252 the decimal value, it gives me a negative as well. I hope this is just a simple solution, and I just can't see it.

提前致谢.

推荐答案

一个字符串转char数组就这么简单

A string to char array is as simple as

String str = "someString"; 
char[] charArray = str.toCharArray();

你能解释一下你想要做什么吗?

Can you explain a little more on what you are trying to do?

* 更新 *

如果我理解您的新评论,您可以使用字节数组并提供示例.

if I am understanding your new comment, you can use a byte array and example is provided.

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

具有以下输出

0x65 0x10 0xf3 0x29

0x65 0x10 0xf3 0x29

这篇关于字符串到字符数组Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆