我制作了将给定值(来自数组指针)拆分为字节的函数。为简单起见,我使用一个字节值。 为什么在打印值时会出现奇怪的数字?
public class CustomIdTokenConverter extends DefaultUserAuthenticationConverter {
private final JwkTokenStore jwkTokenStore;
public CustomIdTokenConverter(String keySetUri) {
this.jwkTokenStore = new JwkTokenStore(keySetUri);
}
@Override
public Authentication extractAuthentication(Map<String, ?> map) {
String idToken = (String) map.get("id_token");
OAuth2AccessToken token = jwkTokenStore.readAccessToken(idToken);
Map<String, Object> claims = token.getAdditionalInformation();
OAuth2RefreshToken refreshToken = token.getRefreshToken();
String principal = (String) claims.get("sub");
List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList("ROLE_USER");
return new CustomAuthenticationData(principal, claims, authorities);
}
}
输出
2552030
0
119944479905023
255
1
70453687222272
0 2
0
0
答案 0 :(得分:1)
如果您的目标是采用任何类型并破坏字节,那么几乎总是这样做的方法是将指向该类型的指针转换为char *
并使用char *
。
这是一个使用精简代码的示例。
#include <iostream>
struct foo
{
int x;
double y;
char z;
};
void writePage(uint16_t address, char *data, uint8_t const len)
{
for (uint8_t dataIndex = 0; dataIndex < len; dataIndex++)
{
std::cout << (int)data[dataIndex] << std::endl;
}
}
int main()
{
uint8_t array[3] = { 255, 20, 30 };
std::cout << int(array[0]) << " " << int(array[1]) << " " << int(array[2]) << std::endl;
writePage(0, reinterpret_cast<char *>(&array[0]), sizeof(array));
foo f;
f.x = 10;
f.y = 20;
f.z = 'g';
std::cout << "Here are the bytes of foo, which has a sizeof(foo) as " << sizeof(foo) << "\n" ;
writePage(0, reinterpret_cast<char *>(&f), sizeof(f));
return 0;
}
输出:
255 20 30
-1
20
30
Here are the bytes of foo, which has a sizeof(foo) as 24
10
0
0
0
0
0
0
0
0
0
0
0
0
0
52
64
103
-54
-117
-54
-2
127
0
0