Discussions related to Visual Prolog
Loffy
Active Member
Posts: 47
Joined: 15 Aug 2019 11:32

integer or string to binary

Unread post by Loffy »

I am missing something simple again re conversion and the strong typing in Visual Prolog.

I wish to load an 8 bit byte of type integer or string into a binary field/buffer. I thought I had found some predicates to do this but the strong typing is causing me problems.

I have found binary concatenation predicates etc so I should be okay with building a buffer.

Does anyone have a hint re the conversion?

Regards,

Loffy
B.Hooijenga
VIP Member
Posts: 57
Joined: 11 Jul 2002 23:01

Re: integer or string to binary

Unread post by B.Hooijenga »

Hello Loffy,

Strings in Visual Prolog use unicode.
These strings must be converted to unsigned8.
Here is an example.
I use it for communicating with a robot.

Code: Select all

        Binary = unicodestring_to_bin("hallo "),         stdio::write(Binary).   predicates     unicodestring_to_bin : (string) -> binary. clauses     unicodestring_to_bin(Source) = Result :-         Binary = binary::createAtomic(convert(byteCount, string::length(Source))),         Result = unicode2binary8(Source, 0, Binary).   predicates     unicode2binary8 : (string Source, core::positive Index, binary) -> binary Result.     %short Scans a unicodestring and puts the characters into a binary8     %detail The characters of the string are first converted to char8     %end clauses     unicode2binary8("", _, Binary) = Binary :-         !.     unicode2binary8(Source, Index, Binary) = Result :-         string::frontChar(Source, First, Last),         Value = string8::mapFromChar(First),         binary::setIndexed_unsigned8(Binary, Index, Value),         Result = unicode2binary8(Last, Index + 1, Binary),         !.     unicode2binary8(_Source, _Index, Binary) = Binary.
About the integers.
Could you be more precise?. An example, perhaps.

Ben
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: integer or string to binary

Unread post by Thomas Linder Puls »

To answer the question we need more information. What is the binary going to be used for? Must it have some format/specific representation of the data?
Regards Thomas Linder Puls
PDC
Loffy
Active Member
Posts: 47
Joined: 15 Aug 2019 11:32

Re: integer or string to binary

Unread post by Loffy »

Thomas & Mr/ms/Mrs Hooijenga,

Thanks for your responses. I have tried the detailed response and it is working.

My IT Professional activities go back a long way; think 1971 and assembler languages. My head is in the 8-bit byte world, hence I think in 8-bit binary's and hexadecimal, arrays/buffers/index registers, loose/no typing; and I have control.

I wish to manipulate bits/bytes/arrays/buffers in files/memory/facts. I am testing some theories and making progress.

So, getting back to my original question. I want to read/write/manipulate 8-bit byte values any way I please, and in large arrays/buffers without Virtual Prolog getting in my way. Please do not get me wrong. Virtual Prolog has many virtues, and I can see quite a number of Virtual Prolog predicates that can help, it is just the strong typing that is causing me issues. I do not want to work with 16/32/64 bit values unless I really need to.

If this is getting off-subject for your discussion forum feel free to contact me at my private e-mail address.

Regards,

Loffy
Loffy
Active Member
Posts: 47
Joined: 15 Aug 2019 11:32

Re: integer or string to binary

Unread post by Loffy »

I have found the issue at the heart of my problem.

Please consider this discussion thread complete.

Regards,

Loffy
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: integer or string to binary

Unread post by Thomas Linder Puls »

If you are dealing with something that has a certain "struct" layout, then you can (most likely) create a functor domain that has a the same layout.

It you are dealing with an array the are a number of array classes that be used to deal with it.

Finally, you can use inputStream_binary/outputStream_binary to fetch/put data sequentially.

You can find additional information about some of this in Memory Handling.

I would be careful about concatenation, it is a source of inefficiency.

Ben's problem could also be solved like this:

Code: Select all

        String8 = string8::mapFromString(String),         BS = outputStream_binary::new(),         BS:writeBytes(convert(pointer, String8), string8::length(String8)),         Binary = BS:getBinary(),         stdio::writef("%\n", Binary2),
Regards Thomas Linder Puls
PDC
Loffy
Active Member
Posts: 47
Joined: 15 Aug 2019 11:32

Re: integer or string to binary

Unread post by Loffy »

Thomas,

Thanks for your reply.

I am an oldish dog, and I am learning new tricks every day, though sometimes slower than a new dog would.

My problem resolved around two points:

1. I did not understand Unicode. Loading a few files into a hex editor fixed that problem.

2. The strong typing in Visual Prolog was at first a real pain, though now after re-visting my code and getting all of my declarations clear, I have fixed those problems and now the strong typing will help me.

I owe you a little understanding of what I am attempting to do, though I will not elaborate further than saying I am attempting to simulate/emulate the operations of a quantum computer. That requires operation at the classical bit level plus some more sophistication.

I still do not know if I can do this, but I will try and it is fun, especially for an old dog in retirement.

Regards,

Loffy
B.Hooijenga
VIP Member
Posts: 57
Joined: 11 Jul 2002 23:01

Re: integer or string to binary

Unread post by B.Hooijenga »

Loffy,

The type unsigned uses 32 bits and an unsigned8 uses 8 bits
Both types are just a sequence of bits.
There is no principal difference.
Visual Prolog uses the bitsize to define them.

Visual Prolog has a class for bit-operations.
The class uses unsigned. But I think you can use them for whatever you want to prove.

Most Visual Prolog application-programmers are using the high level of abstraction VIP allows.
But it is very well possible these days to write more close to the hardware.
As Thomas demonstrates with my program.

Thomas, thank you.

Your solution, inspires me to speeding up some routines.


Kind greetings

Ben
Post Reply