获取int值的完整32位字符串(1)

Java整型补零技巧
本文探讨了Java中Integer.toBinaryString方法的使用,并提供了一种有效的方式将二进制字符串进行补零处理,以确保固定长度输出。通过使用String.format和BigInteger,解决了转换过程中可能遇到的溢出问题。

 

2017年09月07日 14:56:01 perfectnihil 阅读数:694

java中  Integer有一个静态方法

Integer.toBinaryString

 

但是这个方法获取的字符串默认前面会把0去掉,有没有办法把零补齐?

 

方法一 通过字符串的格式化

 

 

public static void main(String args[]){
    int a = 1;
    int b = 33;
    System.out.println(getFullBinaryString(a));
    System.out.println(getFullBinaryString(b));
}
public static String getFullBinaryString(int num){
    String s = Integer.toBinaryString(num);
    String format = String.format("%032d", Long.parseLong(s));
    return format;
}

 

 

00000000000000000000000000000001
00000000000000000000000000100001

 

这种方法的思路是,

1.先把int值转换为无补零的字符串,

2.然后把这个字符串转换为十进制的值

3.接着利用字符串的format方法格式化使前面自动补零

 

但是这种方式有其不足之处,因为我们知道Long的最大数值是

9223 3720 3685 4775 807, 共十九位,也就是说如果二进制的位数超过19位,

就会出问题

1000 0000 0000 0000 0000 换算成十进制是 524288

 

也就是说当你要转换的int值>=524288的时候,就会出问题

 

我们来验证一下

 

public static void main(String args[]){
    int a = 524288;
    System.out.println(getFullBinaryString(a));

}
public static String getFullBinaryString(int num){
    String s = Integer.toBinaryString(num);
    String format = String.format("%032d", Long.parseLong(s));
    return format;
}

 

 

这时候报错

Exception in thread "main" java.lang.NumberFormatException: For input string: "10000000000000000000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:592)
at java.lang.Long.parseLong(Long.java:631)
at test16.Test3.getFullBinaryString(Test3.java:17)
at test16.Test3.main(Test3.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

 

改成524287呢

 

public static void main(String args[]){
    int a = 524287;
    System.out.println(getFullBinaryString(a));

}
public static String getFullBinaryString(int num){
    String s = Integer.toBinaryString(num);
    String format = String.format("%032d", Long.parseLong(s));
    return format;
}

 

打印结果如下:

00000000000001111111111111111111

一切正常

 

 

那么,如果我们非要用这种String.format("%032d",value)的方式来获取32位int字符该怎么办呢

 

如果这样的话,就要想办法能够表示32位的十进制的值。

 

java math下有一个类叫做BigInteger 可以满足我们的需求

我们把上面的方法做一改进

 

 

public static void main(String args[]){
    int a = 524288;
    System.out.println(getFullBinaryString(a));
}
public static String getFullBinaryString(int num){
    String s = Integer.toBinaryString(num);
    String format = String.format("%032d", new  BigInteger(s));
    return format;
}
 
运行
000000000000 1000 0000 0000 0000 0000
这次我们突破了524288的限制。
 
我们再加下极限
 
public static void main(String args[]){
    int a = Integer.MAX_VALUE;
    System.out.println(getFullBinaryString(a));
}
public static String getFullBinaryString(int num){
    String s = Integer.toBinaryString(num);
    String format = String.format("%032d", new  BigInteger(s));
    return format;
}

 

运行
0111 1111 1111 1111 1111 1111 1111 1111
即便是Integer.MAX_VALUE也没有问题!
 
==============================================================
讲到这里,可能有同学会问,为什么格式化的时候不直接格式化成二进制的,
而非要转换成十进制再格式化呢?
 
其实一开始我也是想如果直接格式化岂不是更好,但是,翻了

Formatter类 的API ,貌似只有十进制,八进制,十六进制的格式化方式,没有二进制的

所以,只好先转换成十进制再来格式化。

要从使用 `cJSON` 库创建或解析的 JSON 对象中获取字符串和整数,需要通过对象结构访问其子项,并检查数据项的类型以提取相应的。 ### 获取字符串获取一个键对应的字符串,可以使用 `cJSON_GetObjectItemCaseSensitive` 函数来查找指定键的对象。如果找到且其类型为 `cJSON_String`,则可以通过访问 `valuestring` 成员来获取字符串: ```c cJSON *item = cJSON_GetObjectItemCaseSensitive(root, "key_name"); if (item && cJSON_IsString(item)) { printf("String value: %s\n", item->valuestring); } ``` ### 获取整数 类似地,对于整数类型的,同样使用 `cJSON_GetObjectItemCaseSensitive` 函数查找键并验证类型是否为 `cJSON_Number`,然后访问 `valueint` 成员: ```c cJSON *item = cJSON_GetObjectItemCaseSensitive(root, "key_name"); if (item && cJSON_IsNumber(item)) { printf("Integer value: %d\n", item->valueint); } ``` ### 示例代码 以下是一个完整的示例,展示如何从一个预先解析好的 JSON 对象中提取字符串和整数: ```c #include "cJSON.h" #include <stdio.h> int main() { const char *json_str = "{\"name\":\"Alice\",\"age\":30}"; cJSON *root = cJSON_Parse(json_str); if (root == NULL) { printf("Error parsing JSON.\n"); return -1; } cJSON *name = cJSON_GetObjectItemCaseSensitive(root, "name"); if (name && cJSON_IsString(name)) { printf("Name: %s\n", name->valuestring); } cJSON *age = cJSON_GetObjectItemCaseSensitive(root, "age"); if (age && cJSON_IsNumber(age)) { printf("Age: %d\n", age->valueint); } cJSON_Delete(root); return 0; } ``` 在这个示例中: - 使用 `cJSON_Parse` 解析了一个 JSON 字符串。 - 利用 `cJSON_GetObjectItemCaseSensitive` 和类型检查获取了 `"name"` 的字符串和 `"age"` 的整数。 - 最后调用 `cJSON_Delete` 释放内存[^1]。 ### 注意事项 - 在操作前确保对指针进行有效性检查,避免空指针异常。 - 每次使用完 `cJSON` 对象后必须调用 `cJSON_Delete` 进行清理,以防止内存泄漏[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值