How to generate a large integer from smaller integers?

e.g {67,114,121} should yield a biginteger 4420217. also vice versa...4420217 should be converted back to {67,114,121}..some function similar to Mathematica function FromDigits[list,b]-takes the digits to be given in base b and constructs an integer from the list of its digits.

 采纳的回答

Pure math solution, no bit-bashing:
>> val = 4420217;
>> vec = fix(mod(val./256.^(floor(log2(val)/8):-1:0),256))
vec =
67 114 121
>> sum(vec.*256.^(numel(vec)-1:-1:0))
ans =
4420217

更多回答(1 个)

X = [67,114,121];
sum(X.*256.*(length(X)-1:-1:0))

3 个评论

thanx..but how can i reverse it back again? im workin on ecc algo where i need to encrypt as well as decrypt the same..
You can also use typecast to create the numbers but you have to pad with 0 before doing the typecast. You also need to swap the bytes after doing the typecast.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Numeric Types 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by