Separate bytes that form an integer number?

Asked

Viewed 151 times

4

Assuming I have this positive number:

local uint = 2000;

how can I get the bytes that compose it separately? For example:

print(sepbytes(uint));
-- 7, 208

My attempt:

local function sepbytes(cur)
  local t = {};
  repeat
    cur = cur / 16;
    table.insert(t, 1, cur);
  until cur <= 0
  return table.unpack(t);
end

print(sepbytes(2e3));

Upshot:

0   9.8813129168249e-324    1.5316035021079e-322    2.4703282292062e-321    3.95252516673e-320  6.324040266768e-319 1.0118464426829e-317    1.6189543082926e-316    2.5903268932682e-315    4.144523029229e-314 6.6312368467665e-313    1.0609978954826e-311    1.6975966327722e-310    2.7161546124355e-309    4.3458473798969e-308    6.953355807835e-307 1.1125369292536e-305    1.7800590868058e-304    2.8480945388892e-303    4.5569512622227e-302    7.2911220195564e-301    1.166579523129e-299 1.8665272370064e-298    2.9864435792103e-297    4.7783097267365e-296    7.6452955627784e-295    1.2232472900445e-293    1.9571956640713e-292    3.131513062514e-291 5.0104209000224e-290    8.0166734400359e-289    1.2826677504057e-287    2.0522684006492e-286    3.2836294410387e-285    5.2538071056619e-284    8.4060913690591e-283    1.3449746190495e-281    2.1519593904791e-280    3.4431350247666e-279    5.5090160396266e-278    8.8144256634025e-277    1.4103081061444e-275    2.256492969831e-274 3.6103887517297e-273    5.7766220027675e-272    9.2425952044279e-271    1.4788152327085e-269    2.3661043723335e-268    3.7857669957337e-267    6.0572271931739e-266    9.6915635090782e-265    1.5506501614525e-263    2.481040258324e-262 3.9696644133184e-261    6.3514630613095e-260    1.0162340898095e-258    1.6259745436952e-257    2.6015592699124e-256    4.1624948318598e-255    6.6599917309757e-254    1.0655986769561e-252    1.7049578831298e-251    2.7279326130076e-250    4.3646921808122e-249    6.9835074892995e-248    1.1173611982879e-246    1.7877779172607e-245    2.8604446676171e-244    4.5767114681874e-243    7.3227383490998e-242    1.171638135856e-240 1.8746210173695e-239    2.9993936277913e-238    4.799029804466e-237 7.6784476871456e-236    1.2285516299433e-234    1.9656826079093e-233    3.1450921726549e-232    5.0321474762478e-231    8.0514359619964e-230    1.2882297539194e-228    2.0611676062711e-227    3.2978681700337e-226    5.276589072054e-225 8.4425425152864e-224    1.3508068024458e-222    2.1612908839133e-221    3.4580654142613e-220    5.5329046628181e-219    8.8526474605089e-218    1.4164235936814e-216    2.2662777498903e-215    3.6260443998244e-214    5.8016710397191e-213    9.2826736635506e-212    1.4852277861681e-210    2.3763644578689e-209    3.8021831325903e-208    6.0834930121445e-207    9.7335888194312e-206    1.557374211109e-204 2.4917987377744e-203    3.986877980439e-202 6.3790047687024e-201    1.0206407629924e-199    1.6330252207878e-198    2.6128403532605e-197    4.1805445652168e-196    6.6888713043469e-195    1.0702194086955e-193    1.7123510539128e-192    2.7397616862605e-191    4.3836186980168e-190    7.0137899168269e-189    1.1222063866923e-187    1.7955302187077e-186    2.8728483499323e-185    4.5965573598917e-184    7.3544917758267e-183    1.1767186841323e-181    1.8827498946116e-180    3.0123998313786e-179    4.8198397302058e-178    7.7117435683292e-177    1.2338789709327e-175    1.9742063534923e-174    3.1587301655877e-173    5.0539682649402e-172    8.0863492239044e-171    1.2938158758247e-169    2.0701054013195e-168    3.3121686421112e-167    5.299469827378e-166 8.4791517238048e-165    1.3566642758088e-163    2.170662841294e-162 3.4730605460704e-161    5.5568968737127e-160    8.8910349979403e-159    1.4225655996704e-157    2.2761049594727e-156    3.6417679351564e-155    5.8268286962502e-154    9.3229259140003e-153    1.49166814624e-151  2.3866690339841e-150    3.8186704543745e-149    6.1098727269992e-148    9.7757963631987e-147    1.5641274181118e-145    2.5026038689789e-144    4.0041661903662e-143    6.4066659045859e-142    1.0250665447337e-140    1.640106471574e-139 2.6241703545184e-138    4.1986725672294e-137    6.7178761075671e-136    1.0748601772107e-134    1.7197762835372e-133    2.7516420536595e-132    4.4026272858552e-131    7.0442036573683e-130    1.1270725851789e-128    1.8033161362863e-127    2.885305818058e-126 4.6164893088929e-125    7.3863828942286e-124    1.1818212630766e-122    1.8909140209225e-121    3.025462433476e-120 4.8407398935616e-119    7.7451838296986e-118    1.2392294127518e-116
1.9827670604029e-115    3.1724272966446e-114    5.0758836746313e-113    8.1214138794101e-112    1.2994262207056e-110    2.079081953129e-109 3.3265311250064e-108    5.3224498000102e-107    8.5159196800163e-106    1.3625471488026e-104    2.1800754380842e-103    3.4881207009347e-102    5.5809931214955e-101    8.9295889943928e-100    1.4287342391028e-98 2.2859747825645e-97 3.6575596521033e-96 5.8520954433652e-95 9.3633527093844e-94 1.4981364335015e-92 2.3970182936024e-91 3.8352292697638e-90 6.1363668316222e-89 9.8181869305955e-88 1.5709099088953e-86 2.5134558542324e-85 4.0215293667719e-84 6.434446986835e-83  1.0295115178936e-81 1.6472184286298e-80 2.6355494858076e-79 4.2168791772922e-78 6.7470066836675e-77 1.0795210693868e-75 1.7272337110189e-74 2.7635739376302e-73 4.4217183002084e-72 7.0747492803334e-71 1.1319598848533e-69 1.8111358157653e-68 2.8978173052245e-67 4.6365076883593e-66 7.4184123013748e-65 1.18694596822e-63   1.899113549152e-62  3.0385816786431e-61 4.861730685829e-60  7.7787690973264e-59 1.2446030555722e-57 1.9913648889156e-56 3.1861838222649e-55 5.0978941156238e-54 8.1566305849982e-53 1.3050608935997e-51 2.0880974297595e-50 3.3409558876152e-49 5.3455294201844e-48 8.552847072295e-47  1.3684555315672e-45 2.1895288505075e-44 3.503246160812e-43  5.6051938572993e-42 8.9683101716788e-41 1.4349296274686e-39 2.2958874039498e-38 3.6734198463196e-37 5.8774717541114e-36 9.4039548065783e-35 1.5046327690525e-33 2.407412430484e-32  3.8518598887745e-31 6.1629758220392e-30 9.8607613152626e-29 1.577721810442e-27  2.5243548967072e-26 4.0389678347316e-25 6.4623485355705e-24 1.0339757656913e-22 1.6543612251061e-21 2.6469779601697e-20 4.2351647362715e-19 6.7762635780344e-18 1.0842021724855e-16 1.7347234759768e-15 2.7755575615629e-14 4.4408920985006e-13 7.105427357601e-12  1.1368683772162e-10 1.8189894035459e-09 2.9103830456734e-08 4.6566128730774e-07 7.4505805969238e-06 0.00011920928955078 0.0019073486328125  0.030517578125  0.48828125  7.8125  125

Expected result:

7, 208

3 answers

4


You can use bit operations for this, without touching too much what you were doing:

local function sepbytes(num)
    local t = {};
    local byte = 0;
    repeat
        table.insert(t, 1, bit32.band(bit32.rshift(num, byte), 0xff));
        byte = byte + 8
    until byte > 24
    return table.unpack(t);
end

print(sepbytes(2000));

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

That depends on endianess. If the architecture is different it needs to go from 24 to 0 and not more than 0 until 24. If you can’t guarantee that you will run on a specific architecture you need to figure out what it is and use one or the other, that’s another problem.

You can check more or less bytes, this checks in 0, 8, 16 and 24, ie 4 bytes (32 Bites). Just change where you go.

If you need to discard those that have 0 values not significant, you could filter that further with one condition. I think it would be wrong to do so.

1

Also try to

string.byte(string.pack("i4",uint),1,4)

Adjust 4 to the size of your numbers.

1

With the help of some comments on my other question i managed to simplify my function to return 2 fixed bytes:

local function InParts(num)
    return (num & (0xff << 8)) >> 8, num & 0xff;
end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.