Question convert string to byte array

cardmaker

Member
Joined
Jan 12, 2019
Messages
21
Programming Experience
Beginner
hello, i need to convert a string (every 4 character ) in one byte array, example

string str("30313032303330343035");

while be byte array [0]=0x01; [1]=0x02 etc

how can i convert?
 
The issue is that you are reading that string incorrectly. That string is a series of hex values representing a string of ASCII characters. 0x30 is '0'. 0x31 is '1', etc. So that string seems to represent an ASCII string that contains "0102030405". And from that, you'll want to parse 2 characters at time to get your intended:
C#:
byte value = { 1, 2, 3, 4, 5 }

Seems like a series of parsing steps. Should be simple enough. Or are you looking for a one-liner shortcut?
 
Last edited:
yes you are right abaut the string is a series of hex values representing a string of ASCII characters, i need parse 4 character not 2, example 3031 converted must result in a hex byte 0x01
 
So the first step is to break the string you have into chunks of two characters. What have you done towards achieving that goal? We're here to help but not to just write your code for you when you don't feel like it. You don't know that you can't do something if you don't try. If you try and fail then you'll find plenty of people willing to help you fix your attempt to get it working. If you're not willing to try on your own behalf, you'll find others less willing to try for you either.
 
Back
Top Bottom