我试图从文件中读取无符号整数(存储为连续字节)并将它们转换为整数.我试过这个:
file = File.new(filename,"r")
num = file.read(2).unpack("S") #read an unsigned short
puts num #value will be less than expected
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
有一些Java相当于Convert.FromBase64String其中
将指定的字符串(将二进制数据编码为 base-64 数字)转换为等效的 8 位无符号整数数组。
我试过了:
将不胜感激任何建议!
因此,如果你在无符号数字上加上1,你就会溢出,如果你有一个带有减法的0,你就已经下溢了.这种情况在每种情况下都适用吗?
如果你做5-0:0101 -0000
= 0101 +(1111 + 1)
= 0101 +0000
= 0101 ...这里有一个零,而不是1.如何解释这个案例?有不同的方法吗?
(使用MIPS架构或其他任何东西)
- -编辑
谢谢Amadan.不过,我理解这一部分.我的问题只是零似乎是一个特例.它似乎不遵循正常数字所做的:在上面的例子中,没有执行1.
我正在进行电路设计,目前正在与ALU一起工作并试图实现溢出检测,当这一个案例出现时并不遵循其他人的做法.
我们假设通过减法,第二个操作数在进入ALU(然后添加到第一个操作数)之前被预转换(二进制补码).因此,每当减法的"invert_b"被设置为1时,b被反转并且我们假设我们正在检查的情况是减法,其应该具有1的进位.
下面的代码显示一个以十六进制格式存储为Java字节值的数字,以及有符号和无符号表示形式.这可能不是正确的措辞.我的意思是,这些数字可以是C风格的1字节无符号字符值.如果我正在解释这些值(这里恰好是Java,但可能使用任何语言中的任何有符号类型),那么如果'number'溢出使用符号位,则数字将显示为负数.但是我可以通过ANDFF和0xFF在无符号解释中打印.
我理解AND是如何工作的,但我无法理解ANDing如何呈现为无符号.有人可以解释一下吗?
public class understand_casting {
public static String printbinary(byte val) {
int displayMask = 1 << 7;
String str = new String();
for(int bit = 1; bit <= 8; ++bit) {
str += (val & displayMask) == 0 ? '0' : '1';
val <<= 1; //shift one to left
}
return str;
}
public static void main(String[] args) {
System.out.printf("0x50=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0x50), (byte)0x50, ((byte)0x50 & 0xFF));
System.out.printf("0x88=%s, as int=%d, as unsigned int=%d\n", printbinary((byte)0x88),(byte)0x88, ((byte)0x88 & …Run Code Online (Sandbox Code Playgroud) 如何将long转换为4个字节?我从C程序接收一些输出,它使用unsigned long.我需要读取此输出并将其转换为4个字节.
但是,java使用64位长的有符号长整数.有没有办法进行这种转换?
我编写这段代码只是为了看看如果我将一个负整数放入无符号整数数组中会发生什么.
#include <iostream>
int main()
{
using namespace std;
unsigned int array[4];
array[0]=4;
array[1]=4;
array[2]=2;
array[3]=-2;
cout << array[0] + array[1] + array[2] + array[3] << endl;
unsigned int b;
b=-2;
cout << b <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望在这两种情况下都会发生整数溢出.但是,仅在实际发生的第二种情况下.在第一种情况下,一切都表现得好像它是一个oridinary整数数组,而不是一个无符号整数数组.那么究竟发生了什么导致了这种异常行为.在任何重要的情况下,我的编译器都是gcc 4.8.谢谢您的帮助.编辑:这是我电脑上的输出
8
4294967294
Run Code Online (Sandbox Code Playgroud) 我正在基于MySQL数据库中的现有表创建Java持久性实体Bean(使用NetBeans IDE 8.0.1).我在这个表中遇到了一个类型为"Unsigned TINYINT(3)"的字段.我发现可以执行以下操作来将列的类型定义为unsigned int:
private long foo;
@Column(columnDefinition = "UNSIGNED INT(11)")
public long getFoo()
{
return foo;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个字段如下:
@Size(max = 3)
@Column(name = "WorkingHours", columnDefinition="UNSIGNED TINYINT(3) default '40'")
private Integer workingHours;
Run Code Online (Sandbox Code Playgroud)
将项目部署到我的服务器时收到以下错误:
{"JBAS014671: Failed services" => {"jboss.persistenceunit.\"my-project.ear/my-project-ejb.jar#old-db\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"my-project.ear/my-project-ejb.jar#old-db\": javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [create table ... etc.]
Caused by: javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [create table ... etc.]
Caused by: …Run Code Online (Sandbox Code Playgroud) 这是我的实现,用于检测在尝试添加两个数字时是否发生了无符号的int溢出.
我系统上unsigned int(UINT_MAX)的最大值是4294967295.
int check_addition_overflow(unsigned int a, unsigned int b) {
if (a > 0 && b > (UINT_MAX - a)) {
printf("overflow has occured\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这似乎适用于我尝试过的价值观.
任何流氓案件?您认为利弊是什么?
我有一个字节数组。
bytes[] = [43, 0, 0, -13, 114, -75, -2, 2, 20, 0, 0]
Run Code Online (Sandbox Code Playgroud)
我想在 Java 中将其转换为无符号字节。这就是我所做的:创建一个新数组并使用 & 0xFF 复制值:
this.bytes = new byte[bytes.length];
for (int i=0;i<bytes.length;i++)
this.bytes[i] = (byte) (bytes[i] & 0xFF);
Run Code Online (Sandbox Code Playgroud)
but the values stay negative in the new array as well. what am I doing wrong?
这是我正在尝试编译的函数:
static ssize_t output(t_out_buffer *buf, char const *src, size_t size)
{
size_t osize;
osize = size;
while ((size > 0)
&& (size -= write(buf->fd, src, size) < osize))
{
src += osize - size;
buf->count += osize - size;
osize = size;
}
if (osize < size)
return (T_OUT_BUFFER_ERROR);
else
return (buf->count);
}
Run Code Online (Sandbox Code Playgroud)
和 gcc 的抱怨:
t_out_buffer.c:11:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
&& (size -= write(buf->fd, src, size) < osize))
^
Run Code Online (Sandbox Code Playgroud)
我认为 as sizeis unsigned, …