www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reading bytes and converting to int

reply "joao" <joao.rab hotmail.com> writes:
Hello everyone, I recently discovered the D language.
So, I want to open a file a read 4 bytes and get the int value.

string bytes = f.read(4)

I tried to cast but give me message it was deprecated.

uint value = cast (uint) bytes

If bytes was for example, "\x24\x00\x00\x00" I would like to 
value to be 0x24.
I know how to do this in Python, it is done with struct.unpack.
Thanks.
Aug 25 2012
next sibling parent "nazriel" <spam dzfl.pl> writes:
On Saturday, 25 August 2012 at 15:23:45 UTC, joao wrote:
 Hello everyone, I recently discovered the D language.
 So, I want to open a file a read 4 bytes and get the int value.

 string bytes = f.read(4)

 I tried to cast but give me message it was deprecated.

 uint value = cast (uint) bytes

 If bytes was for example, "\x24\x00\x00\x00" I would like to 
 value to be 0x24.
 I know how to do this in Python, it is done with struct.unpack.
 Thanks.
Maybe std.bitmanip.peek could help?
Aug 25 2012
prev sibling next sibling parent David <d dav1d.de> writes:
Am 25.08.2012 17:23, schrieb joao:
 Hello everyone, I recently discovered the D language.
 So, I want to open a file a read 4 bytes and get the int value.

 string bytes = f.read(4)

 I tried to cast but give me message it was deprecated.

 uint value = cast (uint) bytes

 If bytes was for example, "\x24\x00\x00\x00" I would like to value to be
 0x24.
 I know how to do this in Python, it is done with struct.unpack.
 Thanks.
I think that is what you need/want: http://dlang.org/phobos/std_bitmanip.html#read
Aug 25 2012
prev sibling parent reply "cal" <callumenator gmail.com> writes:
On Saturday, 25 August 2012 at 15:23:45 UTC, joao wrote:
 Hello everyone, I recently discovered the D language.
 So, I want to open a file a read 4 bytes and get the int value.

 string bytes = f.read(4)

 I tried to cast but give me message it was deprecated.

 uint value = cast (uint) bytes

 If bytes was for example, "\x24\x00\x00\x00" I would like to 
 value to be 0x24.
 I know how to do this in Python, it is done with struct.unpack.
 Thanks.
auto i = cast(ubyte[])std.file.read("filename"); int val = std.bitmanip.read!(int, std.system.Endian.littleEndian)(i)
Aug 25 2012
parent reply "joao" <joao.rab hotmail.com> writes:
Thanks everyone, I solved the problem.
I didn't understand these bitmanip methods so I searched and 
found a function to do what I wanted:

string b = "\x24\x10\x00\x00";
uint i = byteToInt(b);
uint byteToInt(string b) {
	 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
}

=> 4132
Aug 25 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, August 25, 2012 21:50:58 joao wrote:
 Thanks everyone, I solved the problem.
 I didn't understand these bitmanip methods so I searched and
 found a function to do what I wanted:
 
 string b = "\x24\x10\x00\x00";
 uint i = byteToInt(b);
 uint byteToInt(string b) {
 	 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
 }
 
 => 4132
What's hard to understand about them? I thought that they were very straightforward. This will do essentially the same thing: import std.bitmanip; import std.system; void main() { ubyte[] b = [0x24, 0x10, 0x00, 0x00]; auto i = peek!(uint, Endian.littleEndian)(b); assert(i == 4132); } Use peek if you don't want to consume the buffer and read if you do. - Jonathan M Davis
Aug 25 2012
parent reply "joao" <joao.rab hotmail.com> writes:
On Saturday, 25 August 2012 at 20:03:38 UTC, Jonathan M Davis 
wrote:
 On Saturday, August 25, 2012 21:50:58 joao wrote:
 Thanks everyone, I solved the problem.
 I didn't understand these bitmanip methods so I searched and
 found a function to do what I wanted:
 
 string b = "\x24\x10\x00\x00";
 uint i = byteToInt(b);
 uint byteToInt(string b) {
 	 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
 }
 
 => 4132
What's hard to understand about them? I thought that they were very straightforward. This will do essentially the same thing: import std.bitmanip; import std.system; void main() { ubyte[] b = [0x24, 0x10, 0x00, 0x00]; auto i = peek!(uint, Endian.littleEndian)(b); assert(i == 4132); } Use peek if you don't want to consume the buffer and read if you do. - Jonathan M Davis
It's not hard, sorry, it's because I'm a begginner. So, all I wanted to do was to open a file and read the first 4 bytes and then return the int value. The last part I managed to do. But the first I'm trying. In Python it is something like this: f = open('filename') s = f.read(4) ... In D I don't know yet. I'm trying to do: File f = File('filename') Then I want to read just 4 bytes. I still don't know what method is :P
Aug 25 2012
next sibling parent David <d dav1d.de> writes:
Am 25.08.2012 22:18, schrieb joao:
 On Saturday, 25 August 2012 at 20:03:38 UTC, Jonathan M Davis wrote:
 On Saturday, August 25, 2012 21:50:58 joao wrote:
 Thanks everyone, I solved the problem.
 I didn't understand these bitmanip methods so I searched and
 found a function to do what I wanted:

 string b = "\x24\x10\x00\x00";
 uint i = byteToInt(b);
 uint byteToInt(string b) {
      return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
 }

 => 4132
What's hard to understand about them? I thought that they were very straightforward. This will do essentially the same thing: import std.bitmanip; import std.system; void main() { ubyte[] b = [0x24, 0x10, 0x00, 0x00]; auto i = peek!(uint, Endian.littleEndian)(b); assert(i == 4132); } Use peek if you don't want to consume the buffer and read if you do. - Jonathan M Davis
It's not hard, sorry, it's because I'm a begginner. So, all I wanted to do was to open a file and read the first 4 bytes and then return the int value. The last part I managed to do. But the first I'm trying. In Python it is something like this: f = open('filename') s = f.read(4) ... In D I don't know yet. I'm trying to do: File f = File('filename') Then I want to read just 4 bytes. I still don't know what method is :P
File f = File("filename"); auto buf = f.rawRead(new ubyte[4]);
Aug 25 2012
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, August 25, 2012 22:18:50 joao wrote:
 It's not hard, sorry, it's because I'm a begginner.
 So, all I wanted to do was to open a file and read the first 4
 bytes and then return the int value. The last part I managed to
 do.
 But the first I'm trying.
 
 In Python it is something like this:
 f = open('filename')
 s = f.read(4)
 ...
 In D I don't know yet. I'm trying to do:
 File f = File('filename')
 Then I want to read just 4 bytes. I still don't know what method
 is :P
I believe that this will work; import std.stdio; auto file = File("filename"); auto buffer = ubyte[](4); buffer = file.rawRead(buffer); rawRead will read in the type given, and it will read up to the number of elements given (less if the file is shorter). If the elements are ubyte, then the length of the array before passing it in will be the number of bytes to read, and the length of the result is the number of bytes read. - Jonathan M Davis
Aug 25 2012
parent reply "joao" <joao.rab hotmail.com> writes:
On Saturday, 25 August 2012 at 20:31:22 UTC, Jonathan M Davis 
wrote:
 On Saturday, August 25, 2012 22:18:50 joao wrote:
 It's not hard, sorry, it's because I'm a begginner.
 So, all I wanted to do was to open a file and read the first 4
 bytes and then return the int value. The last part I managed to
 do.
 But the first I'm trying.
 
 In Python it is something like this:
 f = open('filename')
 s = f.read(4)
 ...
 In D I don't know yet. I'm trying to do:
 File f = File('filename')
 Then I want to read just 4 bytes. I still don't know what 
 method
 is :P
I believe that this will work; import std.stdio; auto file = File("filename"); auto buffer = ubyte[](4); buffer = file.rawRead(buffer); rawRead will read in the type given, and it will read up to the number of elements given (less if the file is shorter). If the elements are ubyte, then the length of the array before passing it in will be the number of bytes to read, and the length of the result is the number of bytes read. - Jonathan M Davis
Ok, so I tried both ways and gave errors: import std.stdio, std.cstream, std.system, std.bitmanip; File f = File("filename"); auto buf = f.rawRead(new ubyte[4]); auto i = peek!(uint, Endian.littleEndian)(buf); std.stdio.File conflicts with std.stream.File and auto file = File("filename"); auto buffer = ubyte[](4); buffer = file.rawRead(buffer); auto i = peek!(uint, Endian.littleEndian)(buffer); found '[' when expecting '.' following ubyte and ']' when expecting identifier following 'ubyte'
Aug 25 2012
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, August 25, 2012 22:49:18 joao wrote:
 Ok, so I tried both ways and gave errors:
 import std.stdio, std.cstream, std.system, std.bitmanip;
 
 File f = File("filename");
 auto buf = f.rawRead(new ubyte[4]);
 auto i = peek!(uint, Endian.littleEndian)(buf);
 
 std.stdio.File conflicts with std.stream.File
Then either don't import both or use the full import. auto file = std.stdio.File("filename"); But I wouldn't advise using std.stream for anything unless you have to. As the note in its documentation states, it's considered out-of-date and will be replaced.
 and
 
 auto file = File("filename");
 auto buffer = ubyte[](4);
 buffer = file.rawRead(buffer);
 auto i = peek!(uint, Endian.littleEndian)(buffer);
 
 found '[' when expecting '.' following ubyte and ']' when
 expecting identifier following 'ubyte'
It's missing new. My mistake. auto buffer = new ubyte[](4); or as David pointed out, you get just allocate the array in the rawRead call: auto buf = file.rawRead(new ubyte[](4)); - Jonathan M Davis
Aug 25 2012
next sibling parent reply "joao" <joao.rab hotmail.com> writes:
On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis 
wrote:
 On Saturday, August 25, 2012 22:49:18 joao wrote:
 Ok, so I tried both ways and gave errors:
 import std.stdio, std.cstream, std.system, std.bitmanip;
 
 File f = File("filename");
 auto buf = f.rawRead(new ubyte[4]);
 auto i = peek!(uint, Endian.littleEndian)(buf);
 
 std.stdio.File conflicts with std.stream.File
Then either don't import both or use the full import. auto file = std.stdio.File("filename"); But I wouldn't advise using std.stream for anything unless you have to. As the note in its documentation states, it's considered out-of-date and will be replaced.
 and
 
 auto file = File("filename");
 auto buffer = ubyte[](4);
 buffer = file.rawRead(buffer);
 auto i = peek!(uint, Endian.littleEndian)(buffer);
 
 found '[' when expecting '.' following ubyte and ']' when
 expecting identifier following 'ubyte'
It's missing new. My mistake. auto buffer = new ubyte[](4); or as David pointed out, you get just allocate the array in the rawRead call: auto buf = file.rawRead(new ubyte[](4)); - Jonathan M Davis
It worked! Thanks. Just one more question, I and to do the reverse. Like 4132 => "\x24\x10"
Aug 25 2012
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Sunday, August 26, 2012 01:25:04 joao wrote:
 It worked! Thanks.
 Just one more question, I and to do the reverse. Like 4132 =>
 "\x24\x10"
Use std.bitmanip.write: http://dlang.org/phobos/std_bitmanip.html#write - Jonathan M Davis
Aug 25 2012
prev sibling parent reply "Tommi" <tommitissari hotmail.com> writes:
On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis 
wrote:
 auto buf = file.rawRead(new ubyte[](4));
Could we somehow skip making the temporary buffer, and read from the file directly into an existing variable. Can we make a slice of one element that points to an existing value. import std.stdio; struct BigValue // has no indirection { int m_value1; int m_value2; long m_value3; // ... } BigValue g_bigValue; void fun() { auto file = File("filename"); // How to create a slice of size 1 which references g_bigValue? BigValue[] refToBigValue /* = ? */ ; buffer = file.rawRead(refToBigValue); }
Aug 26 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 08/26/2012 04:19 PM, Tommi wrote:
 On Saturday, 25 August 2012 at 20:58:47 UTC, Jonathan M Davis wrote:
 auto buf = file.rawRead(new ubyte[](4));
Could we somehow skip making the temporary buffer, and read from the file directly into an existing variable. Can we make a slice of one element that points to an existing value. import std.stdio; struct BigValue // has no indirection { int m_value1; int m_value2; long m_value3; // ... } BigValue g_bigValue; void fun() { auto file = File("filename"); // How to create a slice of size 1 which references g_bigValue? BigValue[] refToBigValue /* = ? */ ;
auto refToBigValue = (&g_bigValue)[0..1];
      buffer = file.rawRead(refToBigValue);
 }
Aug 26 2012
parent "Tommi" <tommitissari hotmail.com> writes:
On Sunday, 26 August 2012 at 15:18:27 UTC, Timon Gehr wrote:
 auto refToBigValue = (&g_bigValue)[0..1];
Thanks. Oughta read the f***ing manual instead of glancing through it: http://dlang.org/arrays.html
Aug 26 2012