www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - ubyte arrays to numbers

↑ ↓ ← lurker <lurker lurker.com> writes:
hi,

is there any good and easy way to convert ubyte arrays to short, ushort, int,
uint long and ulong?

thanks 

lurker
Apr 07 2008
↑ ↓ Leandro Lucarella <llucax gmail.com> writes:
lurker, el  7 de abril a las 11:43 me escribiste:
 hi,
 
 is there any good and easy way to convert ubyte arrays to short, ushort, int,
uint long and ulong?

if the byte order is correct, a cast should do it, if not, htons and htonl should help (for longs, I think you have to roll your own hton). -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- SATANAS EN COMISARIA -- Crónica TV
Apr 07 2008
↑ ↓ lurker <lurker lurker.com> writes:
do you know of any prefab functions of templates?

Leandro Lucarella Wrote:

 lurker, el  7 de abril a las 11:43 me escribiste:
 hi,
 
 is there any good and easy way to convert ubyte arrays to short, ushort, int,
uint long and ulong?

if the byte order is correct, a cast should do it, if not, htons and htonl should help (for longs, I think you have to roll your own hton). -- Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/ ---------------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------------- SATANAS EN COMISARIA -- Crónica TV

Apr 07 2008
↑ ↓ Regan Heath <regan netmail.co.nz> writes:
lurker wrote:
 do you know of any prefab functions of templates?
 
 Leandro Lucarella Wrote:
 
 lurker, el  7 de abril a las 11:43 me escribiste:
 hi,

 is there any good and easy way to convert ubyte arrays to short, ushort, int,
uint long and ulong?

should help (for longs, I think you have to roll your own hton).


import std.stdio; T convert(T)(ubyte[] data) { return *(cast(T*)data[0..T.sizeof].ptr); } void main() { writefln(convert!(ubyte) ([ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])); //2^7 = 128 writefln(convert!(ushort)([ 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])); //2^15 = 32768 writefln(convert!(uint) ([ 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00 ])); //2^31 = 2147483648 writefln(convert!(ulong) ([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 ])); //2^63 = 9223372036854775808 } ** You'll want to un-wrap the array literals if your reader has wrapped the lines. :) Regan
Apr 08 2008
↑ ↓ → lurker <lurker lurker.com> writes:
thank you so much

Regan Heath Wrote:

 lurker wrote:
 do you know of any prefab functions of templates?
 
 Leandro Lucarella Wrote:
 
 lurker, el  7 de abril a las 11:43 me escribiste:
 hi,

 is there any good and easy way to convert ubyte arrays to short, ushort, int,
uint long and ulong?

should help (for longs, I think you have to roll your own hton).


import std.stdio; T convert(T)(ubyte[] data) { return *(cast(T*)data[0..T.sizeof].ptr); } void main() { writefln(convert!(ubyte) ([ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])); //2^7 = 128 writefln(convert!(ushort)([ 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ])); //2^15 = 32768 writefln(convert!(uint) ([ 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00 ])); //2^31 = 2147483648 writefln(convert!(ulong) ([ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80 ])); //2^63 = 9223372036854775808 } ** You'll want to un-wrap the array literals if your reader has wrapped the lines. :) Regan

Apr 08 2008